Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ForbiddenImport matches on substring, not full class #1573

Closed
josephlbarnett opened this issue Apr 2, 2019 · 2 comments
Closed

ForbiddenImport matches on substring, not full class #1573

josephlbarnett opened this issue Apr 2, 2019 · 2 comments
Labels
Milestone

Comments

@josephlbarnett
Copy link
Contributor

josephlbarnett commented Apr 2, 2019

The bug you're experiencing might have already be reported!
Please search in the issues before creating one.

Expected Behavior

There should be a way to forbid imports of specific classes and not match classes that are similar, but different.

Observed Behavior

because ForbiddenImport uses the contains method of SplitPattern, trying to forbid a class whose name is a substring of another class forbids both classes. I believe calling SplitPattern::equals instead would fix the issue, but have not tested, and would probably break other usages (eg the wildcards in #1001 ).

Steps to Reproduce

forbid com.google.inject.Inject, and run detekt against a class that imports com.google.inject.Injector

Context

We'd like to forbid imports of guice classes that exist in javax.inject, for example javax.inject.Inject should be used over com.google.inject.Inject. However, adding com.google.inject.Injectto the ForbiddenImports rule also forbids imports of com.google.inject.Injector, which is a class that has no javax.inject equivalent that we do not want to forbid.

Your Environment

  • Version of detekt used: 1.0.0-RC14
  • Version of Gradle used (if applicable):
  • Operating System and version:
  • Link to your project (if it's a public repository):
@schalkms schalkms added the rules label Apr 3, 2019
@schalkms
Copy link
Member

schalkms commented Apr 3, 2019

Thanks for reporting and having a look into it!
Would you mind to submit a PR including your tests?

josephlbarnett added a commit to josephlbarnett/detekt that referenced this issue Apr 3, 2019
Shows that forbidding net.example.R.dimen also forbids
net.example.R.dimension.
josephlbarnett added a commit to josephlbarnett/detekt that referenced this issue Apr 3, 2019
Shows that forbidding net.example.R.dimen also forbids
net.example.R.dimension.
josephlbarnett added a commit to josephlbarnett/detekt that referenced this issue Apr 3, 2019
@josephlbarnett
Copy link
Contributor Author

PR submitted: #1575

josephlbarnett added a commit to josephlbarnett/detekt that referenced this issue Apr 3, 2019
Shows that forbidding net.example.R.dimen also forbids
net.example.R.dimension.
josephlbarnett added a commit to josephlbarnett/detekt that referenced this issue Apr 3, 2019
josephlbarnett added a commit to josephlbarnett/detekt that referenced this issue Apr 4, 2019
@arturbosch arturbosch added this to the RC15 milestone Apr 7, 2019
arturbosch pushed a commit that referenced this issue Apr 7, 2019
* Test case for issue #1573

Shows that forbidding net.example.R.dimen also forbids
net.example.R.dimension.

* Translate glob patterns to regexes for ForbiddenImport rule

fixes issue #1573
@lock lock bot locked as resolved and limited conversation to collaborators Jul 17, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants