-
Notifications
You must be signed in to change notification settings - Fork 39
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
Conversation
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)
Codecov Report
@@ 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
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter6/UnsafeUseLastIndex.kt
Outdated
Show resolved
Hide resolved
diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter6/UnsafeUseLastIndexFixTest.kt
Outdated
Show resolved
Hide resolved
diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/constants/Warnings.kt
Outdated
Show resolved
Hide resolved
@@ -2713,6 +2713,16 @@ fun SomeClass.deleteAllSpaces() { | |||
} | |||
``` | |||
|
|||
#### <a name="r6.2.4"></a> 6.2.4 Don't use property length with operation - 1 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, done
There was a problem hiding this comment.
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...
diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter6/UnsafeUseLastIndex.kt
Outdated
Show resolved
Hide resolved
info/guide/guide-chapter-6.md
Outdated
@@ -428,6 +428,22 @@ fun SomeClass.deleteAllSpaces() { | |||
} | |||
``` | |||
|
|||
#### <a name="r6.2.4"></a> 6.2.4 Don't use property length with operation - 1 |
There was a problem hiding this comment.
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"
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
info/available-rules.md
Outdated
| Chap | Standard | Rule name | Description | Fix | Config | FixMe | |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please rename to fix
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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"), |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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" |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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)?
### 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)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this 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
UnsafeUseLastIndex
Added warn and fix rule that changes
A.length -1
toA.lastIndex
This pull request closes #1140
Actions checklist