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
Check for root of receiver in selector expression #7220
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,7 +73,12 @@ class VarCouldBeVal(config: Config) : Rule( | |
file.accept(assignmentVisitor) | ||
|
||
assignmentVisitor.getNonReAssignedDeclarations().forEach { | ||
report(CodeSmell(Entity.from(it), "Variable '${it.nameAsSafeName.identifier}' could be val.")) | ||
report( | ||
CodeSmell( | ||
Entity.from(it), | ||
"Variable '${it.nameAsSafeName.identifier}' could be val." | ||
) | ||
) | ||
} | ||
} | ||
|
||
|
@@ -95,7 +100,8 @@ class VarCouldBeVal(config: Config) : Rule( | |
val declarationName = nameAsSafeName.toString() | ||
val assignments = assignments[declarationName] | ||
if (assignments.isNullOrEmpty()) return false | ||
val declarationDescriptor = bindingContext[BindingContext.DECLARATION_TO_DESCRIPTOR, this] | ||
val declarationDescriptor = | ||
bindingContext[BindingContext.DECLARATION_TO_DESCRIPTOR, this] | ||
return assignments.any { | ||
it.getResolvedCall(bindingContext)?.resultingDescriptor?.original == declarationDescriptor || | ||
// inside an unknown types context? (example: with-statement with unknown type) | ||
|
@@ -138,9 +144,9 @@ class VarCouldBeVal(config: Config) : Rule( | |
expression.left?.let(::visitAssignment) | ||
|
||
// Check for whether the assignment contains an object literal. | ||
val descriptor = (expression.left as? KtNameReferenceExpression)?.let { | ||
it.getResolvedCall(bindingContext)?.resultingDescriptor | ||
} | ||
val descriptor = (expression.left as? KtNameReferenceExpression) | ||
?.getResolvedCall(bindingContext) | ||
?.resultingDescriptor | ||
Comment on lines
+147
to
+149
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are you changing this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment
|
||
val expressionRight = expression.right | ||
if (descriptor != null && expressionRight != null) { | ||
evaluateAssignmentExpression(descriptor, expressionRight) | ||
|
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.
Same as other comment. Looks like this change is unnecessary (let's move it to another PR?)
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.
With this rule, this code is started flagging. As we have written the code as
<expression>?.let { it.<someOtherNullableChain> }
that could be simplified as<expression>?.<someOtherNullableChain>