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

Add RedundantMap rule #2982

Closed
wants to merge 4 commits into from
Closed

Conversation

VovaStelmashchuk
Copy link
Contributor

Implement suggest from issue #2975

@codecov
Copy link

codecov bot commented Aug 18, 2020

Codecov Report

Merging #2982 into master will decrease coverage by 0.50%.
The diff coverage is 91.66%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master    #2982      +/-   ##
============================================
- Coverage     80.29%   79.78%   -0.51%     
- Complexity     2478     2515      +37     
============================================
  Files           424      428       +4     
  Lines          7479     7553      +74     
  Branches       1370     1422      +52     
============================================
+ Hits           6005     6026      +21     
- Misses          760      771      +11     
- Partials        714      756      +42     
Impacted Files Coverage Δ Complexity Δ
...arturbosch/detekt/rules/complexity/RedundantMap.kt 90.00% <90.00%> (ø) 8.00 <8.00> (?)
...osch/detekt/rules/complexity/ComplexityProvider.kt 100.00% <100.00%> (ø) 2.00 <0.00> (ø)
...itlab/arturbosch/detekt/core/extensions/Loading.kt 22.22% <0.00%> (-44.45%) 0.00% <0.00%> (ø%)
...lab/arturbosch/detekt/rules/style/MaxLineLength.kt 85.29% <0.00%> (-14.71%) 23.00% <0.00%> (ø%)
...arturbosch/detekt/generator/collection/KDocTags.kt 64.51% <0.00%> (-12.91%) 0.00% <0.00%> (ø%)
...tlin/io/gitlab/arturbosch/detekt/cli/ReportPath.kt 81.81% <0.00%> (-9.10%) 1.00% <0.00%> (ø%)
.../detekt/metrics/processors/ProjectSLOCProcessor.kt 92.30% <0.00%> (-7.70%) 3.00% <0.00%> (ø%)
...itlab/arturbosch/detekt/api/internal/YamlConfig.kt 76.92% <0.00%> (-7.70%) 9.00% <0.00%> (ø%)
...n/kotlin/io/gitlab/arturbosch/detekt/rules/Junk.kt 46.66% <0.00%> (-6.67%) 0.00% <0.00%> (ø%)
...detekt/rules/style/UnderscoresInNumericLiterals.kt 75.86% <0.00%> (-6.29%) 20.00% <0.00%> (ø%)
... and 61 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 d6c5047...54cd2ee. Read the comment docs.

@VovaStelmashchuk VovaStelmashchuk marked this pull request as ready for review August 18, 2020 16:06
""")).isEmpty()
}
}
})
Copy link
Member

Choose a reason for hiding this comment

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

Could you add a test for list.map { foo -> foo }?

Copy link
Member

Choose a reason for hiding this comment

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

and another for

list.map {
  println("hello")
  it
}

this map is useless too. If you want to print hello for each value you should use onEach or forEach instead of map.

/**
* map { it } does not make sense. You can remove the operator without any impact.
* <noncompliant>
* listOf(1, 2).map { it }
Copy link
Member

Choose a reason for hiding this comment

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

Are there other examples where just writing it in the lambda expression does not make sense?
Otherwise, the rule seems to be very basic.

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure but what about a map that returns a constant? should we flag it?

list.map { "hi" } is the same as List(list.size) { "hi" }... I'm not sure.

Anyway, I found this issues in code after multiple refactors. A map that some time was doing something it ends doing nothing.

Comment on lines +49 to +63
it("should not report filter function") {
assertThat(RedundantMap().compileAndLint("""
fun test(list: List<Boolean>) : List<Boolean> {
return list.filter { it }
}
""")).isEmpty()
}

it("should not report filter function") {
assertThat(RedundantMap().compileAndLint("""
fun test(list: List<Boolean>) : List<Boolean> {
return list.filter { it }
}
""")).isEmpty()
}
Copy link
Member

Choose a reason for hiding this comment

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

duplicate

* </compliant>
* @since 1.11.0
*/
class RedundantMap(
Copy link
Member

Choose a reason for hiding this comment

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

How about RedundantHigherOrderMapUsage ?

import org.jetbrains.kotlin.psi.KtLambdaExpression

/**
* map { it } does not make sense. You can remove the operator without any impact.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* map { it } does not make sense. You can remove the operator without any impact.
* Using map { it } indicates a remnant of a recent refactoring. You can remove the operator without any impact.

does not make sense is really strong in this context.

* <compliant>
* listOf(1, 2).map { it * 2 }
* </compliant>
* @since 1.11.0
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* @since 1.11.0
* @since 1.12.0

@arturbosch arturbosch added this to the 1.12.0 milestone Aug 20, 2020
…etekt/rules/complexity/RedundantMap.kt

Co-authored-by: Artur Bosch <arturbosch@gmx.de>
@arturbosch arturbosch modified the milestones: 1.12.0, 1.13.0 Aug 25, 2020
@arturbosch arturbosch removed this from the 1.13.0 milestone Sep 7, 2020
@schalkms
Copy link
Member

I think we should try to get this rule into the next detekt version. It’s a nice addition. Can this merge conflict be handled?

@VovaStelmashchuk
Copy link
Contributor Author

Feature not finished as the moment. Ok I will resolve conflict.

@schalkms
Copy link
Member

@VovaStelmashchuk no worries. I just thought an up-to-date CI feedback might help.

@hbmartin
Copy link
Contributor

@schalkmsn @VovaStelmashchuk is this being actively worked on? I'd be happy to take a look if not.

@VovaStelmashchuk
Copy link
Contributor Author

I close this PR. I will come back to you when my solution be ready

@schalkms
Copy link
Member

@hbmartin please go ahead and feel free to cherry pick the necessary commits. Thank you. 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants