CognitiveComplexMethod false positive: returning anonymous object is complex
#5,560 opened on Nov 22, 2022
Repository metrics
- Stars
- (6,942 stars)
- PR merge metrics
- (Avg merge 4d 9h) (65 merged PRs in 30d)
Description
Expected Behavior
Anonymous objects are ignored, same as https://github.com/detekt/detekt/issues/1037.
Observed Behavior

> Task :checkstyle:detektMain FAILED
checkers\checkstyle\src\main\kotlin\net\twisterrob\gradle\checkstyle\CheckStyleTaskCreator.kt:22:15: The function taskConfigurator appears to be too complex based on Cognitive Complexity (complexity: 17). Defined complexity threshold for methods is set to '15' [CognitiveComplexMethod]
Steps to Reproduce
Implement an interface or base class with an anonymous object literal, and return it from a function.
Context
It seems that the requirements changed, or the split (#5542) regressed #1037; or maybe the cognitive complexity was never supportive of this complexity. When I look at a function that suddenly too complex I look at the first line to get context, at this point (after the initial scare), it's really easy to understand that there's a class declaration in the function. At this point the individual functions inside that class are not relevant for complexity as they're adding to the complexity of the class/object and not the enclosing function.
cc @arturbosch @sanggggg
Your Environment
- Version of detekt used: 1.22.0
- Version of Gradle used (if applicable): 7.5.1
- Gradle scan link (add
--scanoption when running the gradle task): - Operating System and version: Windows 10
- Link to your project (if it's a public repository): https://github.com/TWiStErRob/net.twisterrob.gradle/blob/0a9c9efa8d3430c2b5b161871bc9f53f053d77af/checkers/checkstyle/src/main/kotlin/net/twisterrob/gradle/checkstyle/CheckStyleTaskCreator.kt#L22-L72