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

NPE in NewlinesRule: #1050

Merged
merged 8 commits into from
Sep 22, 2021
Merged

NPE in NewlinesRule: #1050

merged 8 commits into from
Sep 22, 2021

Conversation

Cheshiriks
Copy link
Member

What's done:

### What's done:
* fixed bug in NewlinesRule
Closes #1047
@codecov
Copy link

codecov bot commented Sep 10, 2021

Codecov Report

Merging #1050 (f68ac35) into master (05994e7) will increase coverage by 0.02%.
The diff coverage is n/a.

Impacted file tree graph

@@             Coverage Diff              @@
##             master    #1050      +/-   ##
============================================
+ Coverage     83.40%   83.43%   +0.02%     
  Complexity     2447     2447              
============================================
  Files           102      102              
  Lines          6122     6120       -2     
  Branches       1833     1832       -1     
============================================
  Hits           5106     5106              
+ Misses          273      272       -1     
+ Partials        743      742       -1     
Flag Coverage Δ
unittests 83.43% <ø> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...iktat/ruleset/rules/chapter3/files/NewlinesRule.kt 77.85% <ø> (+0.53%) ⬆️

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 05994e7...f68ac35. Read the comment docs.

lintMethod(
"""
|fun bar(): String {
| val a = it.responseBody!!.name
Copy link
Member

Choose a reason for hiding this comment

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

This code basically didn't trigger an NPE before

Suggested change
| val a = it.responseBody!!.name
| val a = it.responseBody!![0].name

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. I removed this for a test and forgot to return

@@ -461,7 +462,7 @@ class NewlinesRule(configRules: List<RulesConfig>) : DiktatRule(

if (psi.children.isNotEmpty() && !psi.isFirstChildElementType(DOT_QUALIFIED_EXPRESSION) &&
!psi.isFirstChildElementType(SAFE_ACCESS_EXPRESSION)) {
val firstChild = psi.firstChild
val firstChild = if (psi.isFirstChildElementType(ARRAY_ACCESS_EXPRESSION)) psi.firstChild.firstChild else psi.firstChild
Copy link
Member

Choose a reason for hiding this comment

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

Is that just workaround?

I looked at this bug before and think, that the main problem is in these lines:

https://github.com/cqfn/diKTat/blob/37b2478924f69d9f4be205bc46a2a12bbed89fb8/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/NewlinesRule.kt#L469-L471

For now I didn't catch the logic of this if statement - in which cases do we need this, even more it doesn't covered by any test.
Probably, we should modify it or get rid of it at all.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I deleted this piece and. everything continued to work

Copy link
Member

Choose a reason for hiding this comment

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

Yes, it should work, however did you understand the reason, why did we need this before, for which cases?

I can't imagine any other examples for now, which will satisfy this condition, instead of it.responseBody!![0].name, for which it doesn't work

Copy link
Member

@orchestr7 orchestr7 Sep 13, 2021

Choose a reason for hiding this comment

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

Don't get how it should fix anything?

If we had an NPE on the further manipulations val firstChild or psi.firstChild, you should do null-checks and create special logic for it. Not simply hardcode ARRAY_ACCESS_EXPRESSION

### What's done:
* fixed bug in NewlinesRule
Closes #1047
@@ -527,6 +527,18 @@ class NewlinesRuleWarnTest : LintTestBase(::NewlinesRule) {
)
}

@Test
Copy link
Member

Choose a reason for hiding this comment

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

create fixer tests (where WRONG_NEWLINES is fixing the code in such case) to prove that you are right

Copy link
Member

Choose a reason for hiding this comment

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

@Cheshiriks where are tests for fixers?

Copy link
Member

@orchestr7 orchestr7 left a comment

Choose a reason for hiding this comment

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

add more tests, make the logic at least look less hardcoded (create null-checks and describe your workarounds)

"""
|fun bar(): String {
| val a = it.responseBody!![0].name
|}
Copy link
Member

Choose a reason for hiding this comment

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

please add positive scenarios (that will trigger warnings)

### What's done:
* fixed bug in WRONG_NEWLINES
* now don't ask @return on function with name same last reference Expression
Closes #965
### What's done:
* fixed bug in WRONG_NEWLINES
* now don't ask @return on function with name same last reference Expression
Closes #965
Copy link
Member

@orchestr7 orchestr7 left a comment

Choose a reason for hiding this comment

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

@Cheshiriks Cheshiriks merged commit d22d0a4 into master Sep 22, 2021
@petertrr petertrr deleted the feature/npe-array-access-exp branch May 25, 2022 09:07
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.

NPE in NewlinesRule
3 participants