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

Suggest using lastIndex instead of length - 1 for strings #1221

Merged
merged 14 commits into from
Mar 11, 2022

Conversation

Arrgentum
Copy link
Member

@Arrgentum Arrgentum commented Mar 2, 2022

UnsafeUseLastIndex
Added warn and fix rule that changes A.length -1 to A.lastIndex

This pull request closes #1140

Actions checklist

  • Implemented Rule, added Warnings
  • Added tests on checks
  • Added tests on fixers

Alexey Votintsev added 6 commits March 1, 2022 17:15
What's done:
 - Added new rule to replace .length - 1 to .lastIndex
 - Added warn tests
 - Added fix tests

 (#1140)
What's done:
 - Added new rule to replace .length - 1 to .lastIndex
 - Added warn tests
 - Added fix tests

 (#1140)
### What's done:
- Added new rule to replace ".length - 1" to ".lastIndex"
- Added warn tests
- Added fix tests
- Updated Readme (rule 6.2.4)

(#1140)
…lastIndex

# Conflicts:
#	diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter6/UnsafeUseLastIndex.kt
### What's done:
- Added new rule to replace ".length - 1" to ".lastIndex"
- Added warn tests
- Added fix tests
- Updated Readme (rule 6.2.4)

(#1140)
### What's done:
- Added new rule to replace ".length - 1" to ".lastIndex"
- Added warn tests
- Added fix tests
- Updated Readme (rule 6.2.4)

(#1140)
@Arrgentum Arrgentum requested review from petertrr and orchestr7 March 2, 2022 13:01
@codecov
Copy link

codecov bot commented Mar 2, 2022

Codecov Report

Merging #1221 (a405812) into master (970f316) will increase coverage by 0.02%.
The diff coverage is 92.00%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master    #1221      +/-   ##
============================================
+ Coverage     83.84%   83.87%   +0.02%     
- Complexity     2536     2543       +7     
============================================
  Files           103      104       +1     
  Lines          7136     7161      +25     
  Branches       1936     1939       +3     
============================================
+ Hits           5983     6006      +23     
  Misses          352      352              
- Partials        801      803       +2     
Flag Coverage Δ
unittests 83.87% <92.00%> (+0.02%) ⬆️

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

Impacted Files Coverage Δ
...cqfn/diktat/ruleset/rules/chapter6/UseLastIndex.kt 91.30% <91.30%> (ø)
...tlin/org/cqfn/diktat/ruleset/constants/Warnings.kt 98.03% <100.00%> (+0.01%) ⬆️
...cqfn/diktat/ruleset/rules/DiktatRuleSetProvider.kt 96.63% <100.00%> (+0.02%) ⬆️

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 970f316...a405812. Read the comment docs.

@@ -2713,6 +2713,16 @@ fun SomeClass.deleteAllSpaces() {
}
```

#### <a name="r6.2.4"></a> 6.2.4 Don't use property length with operation - 1
Copy link
Member

Choose a reason for hiding this comment

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

You should add description in a specific chapter; the complete guide is then generated from them

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, done

Copy link
Member

Choose a reason for hiding this comment

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

You can remove new text from diktat-coding-convention.md completely and only add it into guide-chapter-6.md. After that, from info directory you can run ./gradlew updateMarkdownDocumentation to update guide and other files automatically. We don't do it regularly, so there might be a lot of changes...

info/guide/diktat-coding-convention.md Show resolved Hide resolved
info/guide/diktat-coding-convention.md Show resolved Hide resolved
Alexey Votintsev added 3 commits March 2, 2022 17:32
### What's done:
- Added new rule to replace ".length - 1" to ".lastIndex"
- Added warn tests
- Added fix tests
- Updated Readme (rule 6.2.4)

(#1140)
### What's done:
- Added new rule to replace ".length - 1" to ".lastIndex"
- Added warn tests
- Added fix tests
- Updated Readme (rule 6.2.4)

(#1140)
@@ -428,6 +428,22 @@ fun SomeClass.deleteAllSpaces() {
}
```

#### <a name="r6.2.4"></a> 6.2.4 Don't use property length with operation - 1
Copy link
Member

Choose a reason for hiding this comment

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

"Use lastIndex in case you need to get latest element of a collection"

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

@@ -428,6 +428,22 @@ fun SomeClass.deleteAllSpaces() {
}
```

#### <a name="r6.2.4"></a> 6.2.4 Don't use property length with operation - 1
You should not use property length with operation - 1, you can change this to lastIndex
Copy link
Member

Choose a reason for hiding this comment

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

Instead of using .length - 1 use built-in Kotlin method lastIndex, it makes the code more readable.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

| Chap | Standard | Rule name | Description | Fix | Config | FixMe |
Copy link
Member

Choose a reason for hiding this comment

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

Why full file is changed? Some changes in end lines?

Copy link
Member

@sanyavertolet sanyavertolet Mar 3, 2022

Choose a reason for hiding this comment

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

Probably \n was replaced with \r\n, yes

Copy link
Member

Choose a reason for hiding this comment

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

Please configure your git to use only LF in commits How to change line-ending settings

lintMethod(
"""
|val A = "AAAAAAAA"
|val D = A.B.C.length - 1
Copy link
Member

@orchestr7 orchestr7 Mar 3, 2022

Choose a reason for hiding this comment

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

could you please also add test for .length - 2 and .length, .length + 1

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

val parent = node.treeParent
val textParent = parent.text.replace(node.text, text)
val newParent = KotlinParser().createNode(textParent)
val grand = parent.treeParent
Copy link
Member

Choose a reason for hiding this comment

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

grand is used only once - no need to create a variable for it

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

}
}

private fun fixup(node: ASTNode) {
Copy link
Member

Choose a reason for hiding this comment

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

please move this method after it's usage changeRight

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

}
}

private fun fixup(node: ASTNode) {
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 to fix

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

class UnsafeUseLastIndex(configRules: List<RulesConfig>) : DiktatRule(
"last-index",
configRules,
listOf(Warnings.UNSAFE_USE_LAST_INDEX)
Copy link
Member

Choose a reason for hiding this comment

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

UNSAFE_USE_LAST_INDEX please rename it to something more accurate: USE_LAST_INDEX

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

@@ -178,6 +178,7 @@ enum class Warnings(
INLINE_CLASS_CAN_BE_USED(true, "6.1.12", "inline class can be used"),
EXTENSION_FUNCTION_WITH_CLASS(false, "6.2.3", "do not use extension functions for the class defined in the same file"),
RUN_IN_SCRIPT(true, "6.5.1", "wrap blocks of code in top-level scope functions like `run`"),
UNSAFE_USE_LAST_INDEX(true, "6.2.4", "length - 1 should be changed to lastIndex"),
Copy link
Member

Choose a reason for hiding this comment

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

Instead of "length - 1" need to use built-in "lastIndex" operation

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

}

private fun fixup(node: ASTNode) {
val text = node.firstChildNode.text.removeSuffix("length") + "lastIndex"
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 change child here first? (getting the text)
Why not to replace it directly in parent?

val text = node.firstChildNode.text.removeSuffix("length") + "lastIndex"
val parent = node.treeParent
val textParent = parent.text.replace(node.text, text)
val newParent = KotlinParser().createNode(textParent)
Copy link
Member

Choose a reason for hiding this comment

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

To create a new node you simply need string with text.
Why do you need to make several changes (first for child, then for parent)?

Alexey Votintsev added 3 commits March 3, 2022 21:34
### What's done:
- Added new rule to replace ".length - 1" to ".lastIndex"
- Added warn tests
- Added fix tests
- Updated Readme (rule 6.2.4)

(#1140)
### What's done:
- Added new rule to replace ".length - 1" to ".lastIndex"
- Added warn tests
- Added fix tests
- Updated Readme (rule 6.2.4)

(#1140)
### What's done:
- Added new rule to replace ".length - 1" to ".lastIndex"
- Added warn tests
- Added fix tests
- Updated Readme (rule 6.2.4)

(#1140)
@Arrgentum Arrgentum requested a review from petertrr March 4, 2022 12:43
@Arrgentum Arrgentum requested a review from orchestr7 March 4, 2022 12:43
Copy link
Member

@petertrr petertrr left a comment

Choose a reason for hiding this comment

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

LGTM

@orchestr7 orchestr7 changed the title Feature/last index Suggest using lastIndex instead of length - 1 for strings Mar 4, 2022
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.

Looks good, but we need to keep in mind #1222

@Arrgentum Arrgentum merged commit aa3c22b into master Mar 11, 2022
@Arrgentum Arrgentum deleted the feature/lastIndex branch March 11, 2022 09:53
@Arrgentum Arrgentum self-assigned this Jun 21, 2022
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.

Suggest using lastIndex instead of length - 1 for strings
5 participants