detekt/detekt

CognitiveComplexMethod false positive: returning anonymous object is complex

Closed

#5,560 opened on Nov 22, 2022

 (4 comments) (1 reaction) (0 assignees)Kotlin (834 forks)batch import
bughelp wantedrulesstale

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

image

> 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

Contributor guide