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 NoNameShadowing rule #3477

Merged
merged 6 commits into from
Feb 20, 2021
Merged

Add NoNameShadowing rule #3477

merged 6 commits into from
Feb 20, 2021

Conversation

t-kameyama
Copy link
Contributor

Fixes #240

@codecov
Copy link

codecov bot commented Feb 16, 2021

Codecov Report

Merging #3477 (f18dc3d) into master (623e7fd) will decrease coverage by 0.19%.
The diff coverage is 67.91%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master    #3477      +/-   ##
============================================
- Coverage     80.28%   80.08%   -0.20%     
- Complexity     2784     2851      +67     
============================================
  Files           454      457       +3     
  Lines          8415     8537     +122     
  Branches       1608     1648      +40     
============================================
+ Hits           6756     6837      +81     
- Misses          789      793       +4     
- Partials        870      907      +37     
Impacted Files Coverage Δ Complexity Δ
.../arturbosch/detekt/core/baseline/BaselineFacade.kt 77.77% <0.00%> (ø) 6.00 <2.00> (ø)
.../arturbosch/detekt/rules/style/UseIsNullOrEmpty.kt 58.66% <58.66%> (ø) 40.00 <40.00> (?)
...urbosch/detekt/rules/bugs/UnreachableCatchBlock.kt 72.22% <72.22%> (ø) 9.00 <9.00> (?)
...osch/detekt/core/baseline/BaselineResultMapping.kt 73.33% <77.77%> (-5.24%) 8.00 <3.00> (+2.00) ⬇️
.../arturbosch/detekt/rules/naming/NoNameShadowing.kt 84.00% <84.00%> (ø) 16.00 <16.00> (?)
...turbosch/detekt/rules/bugs/PotentialBugProvider.kt 100.00% <100.00%> (ø) 3.00 <0.00> (ø)
...b/arturbosch/detekt/rules/naming/NamingProvider.kt 100.00% <100.00%> (ø) 3.00 <0.00> (ø)
...rturbosch/detekt/rules/style/StyleGuideProvider.kt 100.00% <100.00%> (ø) 3.00 <0.00> (ø)
... and 1 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 623e7fd...d230de8. Read the comment docs.

Comment on lines +426 to +427
NoNameShadowing:
active: false
Copy link
Member

Choose a reason for hiding this comment

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

+1 for putting this under naming. Thank you!

Comment on lines 47 to 48
super.visitProperty(property)
report(property)
Copy link
Member

Choose a reason for hiding this comment

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

Today I learned: KtProperty and DestructuringDeclarationEntry are the sealed types of KtVariableDeclaration

import org.jetbrains.kotlin.resolve.BindingContext

/**
* Disallows shadowing variable declarations.
Copy link
Member

Choose a reason for hiding this comment

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

We should elaborate a bit here. I don't know if it's necessary to explain what variable shadowing means. But at least say why it's bad.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).hasSize(1)
assertThat(findings[0]).hasMessage("Name shadowed: k")
}
Copy link
Member

Choose a reason for hiding this comment

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

Does this implementation check the double it when you nest two lambdas?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

assertThat(findings).hasSize(1)
assertThat(findings[0]).hasMessage("Name shadowed: it")
}

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
it("report shadowing nested lambda implicit 'it' parameter") {
val code = """
fun test() {
listOf(1).forEach {
listOf(2).forEach {
}
}
}
"""
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).hasSize(1)
assertThat(findings[0]).hasMessage("Name shadowed: it")
}

Do this have sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in fec06b6 and 085ab96.

t-kameyama and others added 2 commits February 18, 2021 09:22
…t/rules/naming/NoNameShadowingSpec.kt

Co-authored-by: Brais Gabín <braisgabin@gmail.com>
report(parameter)
}

private fun report(declaration: KtNamedDeclaration) {
Copy link
Member

Choose a reason for hiding this comment

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

Please rename this function. report is a method of the superclass and is doing the actual issue reporting. Here you're instead checking for diagnostics and eventually reporting.

This was referenced Mar 11, 2021
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.

Rule: Shadowed names
5 participants