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

Avoid false positives in MemberNameEqualsClassName #4329

Merged
merged 1 commit into from
Nov 25, 2021
Merged

Conversation

BraisGabin
Copy link
Member

If we can't infer if it's a factory or not assume that it's ok. False-negatives are better than false-positives.

This rule shoul be moved to type-solving only but that's a huge change that probably can be done for 2.0

fixes #4070

@BraisGabin BraisGabin added this to the 1.19.0 milestone Nov 23, 2021
@codecov
Copy link

codecov bot commented Nov 23, 2021

Codecov Report

Merging #4329 (e623dec) into main (03f9b97) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##               main    #4329   +/-   ##
=========================================
  Coverage     84.22%   84.22%           
- Complexity     3258     3259    +1     
=========================================
  Files           474      474           
  Lines         10322    10323    +1     
  Branches       1825     1826    +1     
=========================================
+ Hits           8694     8695    +1     
  Misses          666      666           
  Partials        962      962           
Impacted Files Coverage Δ
...h/detekt/rules/naming/MemberNameEqualsClassName.kt 87.80% <100.00%> (+0.30%) ⬆️

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 03f9b97...e623dec. Read the comment docs.

}

private fun isFactoryMethod(function: KtNamedFunction, klass: KtClass): Boolean {
private fun isFactoryMethod(function: KtNamedFunction, klass: KtClass): Boolean? {
Copy link
Member

Choose a reason for hiding this comment

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

Boolean -> Boolean? sounds like a code smell to me.
Are we able to invert the logic here and make return true if is a Factory and false if we either don't know or it's not?

Copy link
Member Author

Choose a reason for hiding this comment

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

Why do you think this is a code smell? The point of null to have a value for "we don't know" and it seems like a good usage of null.

Are we able to invert the logic here and make return true if is a Factory and false if we either don't know or it's not?

It should be the other way around: I want to the same if we know that it is a factory or if we don't know. So it should be: false if it is not a factory and true if we don't know or if it is a factory. And return true if "we don't know" seems odd. For that reason I introduced the null. I can change it for true if you think it is better. This is a private function so the correction of what it returns is not that important.

Copy link
Member

Choose a reason for hiding this comment

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

Why do you think this is a code smell?

I think it's maybe more a matter of taste, but I'm in general not a big fan of Boolean?. It forces you to use == false/true in conditions and is not immediately obvious for the reader why the logic is ternary rather than binary.

It should be the other way around

Yup you're right. My point was anyway to restrict to be a Boolean and treat the absence of value as one of the two cases.

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 it's maybe more a matter of taste, but I'm in general not a big fan of Boolean?. It forces you to use == false/true in conditions and is not immediately obvious for the reader why the logic is ternary rather than binary.

Agree. I changed the code for a Boolean and I added a comment in the code to make clear that we don't know but we are assuming that it's a factory to avoid false-positives.

@BraisGabin BraisGabin merged commit adcca36 into main Nov 25, 2021
@BraisGabin BraisGabin deleted the fix-4070 branch November 25, 2021 13:25
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.

MemberNameEqualsClassName false positive for a factory function with implicit return type
2 participants