Skip to content

[VarCouldBeVal] fix overrides false positives - #4664

Merged
marschwar merged 1 commit into
detekt:mainfrom
meriouma:fix-varcouldbeval
Apr 6, 2022
Merged

marschwar merged 1 commit into
detekt:mainfrom
meriouma:fix-varcouldbeval

Conversation

@meriouma

@meriouma meriouma commented Apr 1, 2022 •

Copy link
Copy Markdown
Contributor

I'm not sure this is correct as this seems a simple check, but this fixes #4661 and does not break other tests.

@marschwar marschwar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thank you for submitting the PR.

While I agree, that a lateinit var can never really become a val, I think the underlying issue here is the magic done by the framework. To detekt the variable never changes its value and therefor it could be val. Therefor changing your config to

  VarCouldBeVal:
    active: true
    ignoreAnnotated: ['Inject']

would in my opinion also be a valid fix.

Long story short - I think your approach here is valid. The issue that a private lateinit var is never assigned a value should be handled by a different rule.

@codecov

codecov Bot commented Apr 1, 2022 •

Copy link
Copy Markdown

Codecov Report

Merging #4664 (b65dbd5) into main (0be7775) will not change coverage.
The diff coverage is 100.00%.

@@            Coverage Diff            @@
##               main    #4664   +/-   ##
=========================================
  Coverage     84.58%   84.58%           
  Complexity     3434     3434           
=========================================
  Files           492      492           
  Lines         11329    11329           
  Branches       2058     2058           
=========================================
  Hits           9583     9583           
  Misses          701      701           
  Partials       1045     1045           
Impacted Files Coverage Δ
...lab/arturbosch/detekt/rules/style/VarCouldBeVal.kt 83.50% <100.00%> (ø)

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 0be7775...b65dbd5. Read the comment docs.

@meriouma
meriouma force-pushed the fix-varcouldbeval branch from f8b505c to 8d81554 Compare April 1, 2022 21:23
@meriouma

meriouma commented Apr 1, 2022

Copy link
Copy Markdown
Contributor Author

Thank you for submitting the PR.

While I agree, that a lateinit var can never really become a val, I think the underlying issue here is the magic done by the framework. To detekt the variable never changes its value and therefor it could be val. Therefor changing your config to

  VarCouldBeVal:
    active: true
    ignoreAnnotated: ['Inject']

would in my opinion also be a valid fix.

Long story short - I think your approach here is valid. The issue that a private lateinit var is never assigned a value should be handled by a different rule.

I ended up doing this workaround for the Inject annotation as this was the main issue in one of our project. But there are many frameworks that rely on an annotation and a lateinit var field, like Mockito. So instead of ignoring all the annotations, I thought lateinit could be ignored by the rule. I believe there's already the rule LateinitUsage to check for lateinit anyway?

@BraisGabin

Copy link
Copy Markdown
Member

I don't think that we should ignore lateinit var by default. They are var so if they aren't assigned they should be flagged. We have methods to disable properties with annotations. At most I would say to add a configuration, but i would like to see a user case where it is needed. Dagger or mockito doesn't need this to work.

@marschwar

Copy link
Copy Markdown
Contributor

@meriouma Could you please change this PR to address the issue with override only. I believe that part is not controversial.

I can see the arguments on either side. Since there is an easy workaround for your specific problem I would also not change it at the moment. Maybe this becomes a pattern and we could change it in the future.

@meriouma
meriouma force-pushed the fix-varcouldbeval branch from 8d81554 to 85a6e3d Compare April 4, 2022 15:25
@meriouma meriouma changed the title [VarCouldBeVal] fix overrides and lateinit false positives [VarCouldBeVal] fix overrides false positives Apr 4, 2022
@meriouma

meriouma commented Apr 4, 2022

Copy link
Copy Markdown
Contributor Author

@marschwar Done!

@marschwar

Copy link
Copy Markdown
Contributor

@meriouma Thanks again for picking this up. I would like us to merge #4575 first to avoid a moving target there and would ask your to adopt your test case to junit if that is ok. I am sorry for the inconvenience.

@meriouma

meriouma commented Apr 5, 2022

Copy link
Copy Markdown
Contributor Author

@marschwar Sure, no problem. I can rebase and rewrite the test case once #4575 is merged.

@meriouma
meriouma force-pushed the fix-varcouldbeval branch from 85a6e3d to b65dbd5 Compare April 5, 2022 18:31

@schalkms schalkms left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

PR #4575 has been merged.

@meriouma

meriouma commented Apr 5, 2022 •

Copy link
Copy Markdown
Contributor Author

I've rebased and updated the test.

@BraisGabin BraisGabin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

@marschwar
marschwar merged commit 50f5f5f into detekt:main Apr 6, 2022
@meriouma
meriouma deleted the fix-varcouldbeval branch April 6, 2022 20:49
@cortinico cortinico added this to the 1.20.0 milestone Apr 6, 2022
@cortinico cortinico added the rules label Apr 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

VarCouldBeVal on interface override

5 participants