Skip to content

Implement ignoreAnnotated as a core feature #4102

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

Merged
merged 4 commits into from
Oct 2, 2021

Conversation

BraisGabin
Copy link
Member

@BraisGabin BraisGabin commented Sep 11, 2021

Discussion here: #4068

This id the first step for a more advance way of suppress findings. It implements the most common request one and probably the easiest: ignoreAnnotated. And I implemented it in a way that adding more should be easy.

The main problem that I see with this implementation is the discoverability. As an user this is an interesting feature but it is really difficult to find it. For sure we should add somerhing in our documentation but I think that it would not be enought. Any ideas here?

In this PR I'm removing some default values. For that reason I created #4101 before. We don't want those default values, they are library specific.

And I changed LongParameterList a bit too. It was using the same configuration to do two things: 1) ignore a function completely for an annotation and 2) ignore the annotated parameter for the total count. So I removed the first part of that feature to relay on the common one and changed the second one to use ignoreAnnotatedParameter instead of ignoreAnnotated. This is a breaking change.

Right now I'm just treating the annotations as a name, I'm not using the full qualifier. Do you think that it is necesary to do that? I think that we could keep it like this for now and we could change it later if someone miss that. We should not have problem extending it.

Closes #2231
Closes #4062

@BraisGabin BraisGabin force-pushed the general-annotation-suppress branch 3 times, most recently from 9cdebfe to 671f4de Compare September 11, 2021 14:06
@BraisGabin BraisGabin changed the base branch from main to remove-library-specific-configurations September 11, 2021 14:07
@BraisGabin BraisGabin marked this pull request as draft September 11, 2021 14:07
@BraisGabin BraisGabin force-pushed the general-annotation-suppress branch 3 times, most recently from 064a17e to 4c4e217 Compare September 11, 2021 14:46
@codecov
Copy link

codecov bot commented Sep 11, 2021

Codecov Report

Merging #4102 (27b24fa) into main (f920504) will increase coverage by 0.01%.
The diff coverage is 86.20%.

❗ Current head 27b24fa differs from pull request most recent head 1a8f3d8. Consider uploading reports for the commit 1a8f3d8 to get more accurate results
Impacted file tree graph

@@             Coverage Diff              @@
##               main    #4102      +/-   ##
============================================
+ Coverage     83.45%   83.46%   +0.01%     
+ Complexity     3185     3177       -8     
============================================
  Files           463      465       +2     
  Lines          9095     9103       +8     
  Branches       1768     1774       +6     
============================================
+ Hits           7590     7598       +8     
  Misses          571      571              
  Partials        934      934              
Impacted Files Coverage Δ
...b/arturbosch/detekt/rules/complexity/LongMethod.kt 88.63% <ø> (+1.13%) ⬆️
...bosch/detekt/rules/complexity/LongParameterList.kt 83.33% <50.00%> (-1.12%) ⬇️
...otlin/io/gitlab/arturbosch/detekt/core/Analyzer.kt 80.95% <80.00%> (-0.30%) ⬇️
...ch/detekt/core/suppressors/AnnotationSuppressor.kt 90.00% <90.00%> (ø)
...ab/arturbosch/detekt/core/config/ValidateConfig.kt 100.00% <100.00%> (ø)
.../arturbosch/detekt/core/suppressors/Suppressors.kt 100.00% <100.00%> (ø)
...tlab/arturbosch/detekt/rules/bugs/LateinitUsage.kt 90.00% <100.00%> (ø)
...b/arturbosch/detekt/rules/naming/FunctionNaming.kt 100.00% <100.00%> (ø)
...etekt/rules/style/FunctionOnlyReturningConstant.kt 94.87% <100.00%> (ø)
...sch/detekt/rules/style/UnnecessaryAbstractClass.kt 82.92% <100.00%> (ø)
... and 3 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4e265df...1a8f3d8. Read the comment docs.

@BraisGabin BraisGabin force-pushed the general-annotation-suppress branch 2 times, most recently from 1b63c10 to 89f989d Compare September 11, 2021 17:52
@BraisGabin BraisGabin marked this pull request as ready for review September 11, 2021 17:53
@BraisGabin BraisGabin force-pushed the general-annotation-suppress branch from 89f989d to 51a9a76 Compare September 11, 2021 17:54
Copy link
Member

@cortinico cortinico left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! I believe on the discoverability we can just have a page in the documentation and link it in the release notes

@cortinico cortinico added the notable changes Marker for notable changes in the changelog label Sep 13, 2021
@cortinico cortinico added this to the 1.19.0 milestone Sep 13, 2021
Copy link
Contributor

@marschwar marschwar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like where this is headed.

@@ -136,6 +137,15 @@ internal class Analyzer(
}
}

private fun filterSuppressedFindings(rule: BaseRule): List<Finding> {
val suppressors = getSuppressors(rule)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I read this correctly, the list of suppressors would be generated for each and every file even though it only depends on the rule. This will create a lot of unnecessary objects in the process.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. But the same happens right now with the rules. We create all the rules for each file. Changing this is a big refactor. It will be fixed in 2.0

Base automatically changed from remove-library-specific-configurations to main September 14, 2021 09:06
@BraisGabin BraisGabin force-pushed the general-annotation-suppress branch 4 times, most recently from 80cb167 to 4135639 Compare September 17, 2021 10:21
@BraisGabin BraisGabin force-pushed the general-annotation-suppress branch from 4135639 to 1269026 Compare September 17, 2021 11:00
*/
typealias Suppressor = (Finding) -> Boolean

internal fun getSuppressors(rule: BaseRule): List<Suppressor> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only have one Suppressor instance in the returned list - Would you consider using the decorator pattern in case we have more Suppressors or use List<Suppressor>?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know what do you mean by "decorator pattern".

Right now there is only one suppressor but my idea is to have more once we merge this. There is a short list of examples at #4068

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

open abstract class Suppressor : Predicate<Finding> {
}

operator fun Suppressor.plus(suppressor: Suppressor): Suppressor {
    return object : Suppressor() {
        override fun test(t: Finding) = this@plus.test(t) || suppressor.test(t)
    }
}

object DefaultSuppressor : Suppressor() {
    override fun test(t: Finding) = true
}

I meant like this where we can compose suppressors in a chain (Probably should be called as a chain of responsibility). The benefit of so is that we can simplify the suppressor implementation that to the Analyzer, the suppressor function is always a simple function (Finding) -> Boolean.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that I prefer to handle this as a List. The good part is that all of these isn't an public API so we could change it if we see problems with the current implementation.

@BraisGabin BraisGabin force-pushed the general-annotation-suppress branch from 1378a90 to 1a8f3d8 Compare September 29, 2021 11:07
@BraisGabin BraisGabin added the migration Marker to add a migration step in the changelog label Sep 29, 2021
@BraisGabin BraisGabin merged commit f12697d into main Oct 2, 2021
@BraisGabin BraisGabin deleted the general-annotation-suppress branch October 2, 2021 08:47
@IgorMaineti
Copy link

Hello! Are there any plans on releasing a new version of detekt with this change?

@cortinico
Copy link
Member

Hello! Are there any plans on releasing a new version of detekt with this change?

I guess we can kickoff the 1.19.x release process (with a RC1) sometime this weekend or so 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
migration Marker to add a migration step in the changelog notable changes Marker for notable changes in the changelog suppressors
Projects
None yet
Development

Successfully merging this pull request may close these issues.

MagicNumber: Support for ignoreAnnotated UnusedPrivateMember should ignore methods annotated with @PostConstruct / @PreDestroy
6 participants