Skip to content

INSP: Add E0510 check #1

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Use of this source code is governed by the MIT license that can be
* found in the LICENSE file.
*/

package org.rust.ide.inspections

import com.intellij.psi.util.descendants
import org.rust.lang.core.psi.RsBinaryExpr
import org.rust.lang.core.psi.RsBlockExpr
import org.rust.lang.core.psi.RsExpr
import org.rust.lang.core.psi.RsExprStmt
import org.rust.lang.core.psi.RsMatchExpr
import org.rust.lang.core.psi.RsPathExpr
import org.rust.lang.core.psi.RsUnaryExpr
import org.rust.lang.core.psi.RsVisitor
import org.rust.lang.core.psi.ext.isAssignBinaryExpr
import org.rust.lang.utils.RsDiagnostic
import org.rust.lang.utils.addToHolder

/**
* Inspection that detects the E0510 error.
*/
class RsAssignInMatchGuardInspection : RsLocalInspectionTool() {
override fun buildVisitor(holder: RsProblemsHolder, isOnTheFly: Boolean): RsVisitor = object : RsVisitor() {
override fun visitMatchExpr(match: RsMatchExpr) = inspect(holder, match)

}
}

private fun inspect(holder: RsProblemsHolder, match: RsMatchExpr) {
val varName = getVarName(match.expr) ?: return
match.matchBody?.matchArmList?.forEach { arm ->
(arm.matchArmGuard?.expr as? RsBlockExpr)?.let { guard ->
guard.descendants().forEach { element ->
(element as? RsExprStmt)?.expr.let { expr ->
(expr as? RsBinaryExpr)?.takeIf { it.isAssignBinaryExpr }?.let { binaryExpr ->
getVarName(binaryExpr.left).takeIf { it == varName }?.let {
RsDiagnostic.AssignInMatchGuardError(binaryExpr).addToHolder(holder)
}
}
}
}
}
}
}

private fun getVarName(expr: RsExpr?): String? {
return when (expr) {
is RsPathExpr -> expr.path.referenceName
is RsUnaryExpr -> expr.expr?.let { getVarName(it) }
else -> null
}
}
12 changes: 11 additions & 1 deletion src/main/kotlin/org/rust/lang/utils/RsDiagnostic.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1626,6 +1626,16 @@ sealed class RsDiagnostic(
fixes = listOf(AddAssocTypeBindingsFix(element, missingTypes.map { it.name }))
)
}

class AssignInMatchGuardError(
element: PsiElement
) : RsDiagnostic(element) {
override fun prepare() = PreparedAnnotation(
ERROR,
E0510,
"The matched value was assigned in a match guard"
)
}
}

enum class RsErrorCode {
Expand All @@ -1634,7 +1644,7 @@ enum class RsErrorCode {
E0200, E0201, E0220, E0252, E0254, E0255, E0259, E0260, E0261, E0262, E0263, E0267, E0268, E0277,
E0308, E0322, E0328, E0364, E0365, E0379, E0384,
E0403, E0404, E0407, E0415, E0416, E0424, E0426, E0428, E0429, E0430, E0431, E0433, E0434, E0435, E0437, E0438, E0449, E0451, E0463,
E0517, E0518, E0537, E0552, E0554, E0562, E0569, E0583, E0586, E0594,
E0510, E0517, E0518, E0537, E0552, E0554, E0562, E0569, E0583, E0586, E0594,
E0601, E0603, E0614, E0616, E0618, E0624, E0658, E0666, E0667, E0688, E0695,
E0703, E0704, E0728, E0732, E0733, E0741, E0742, E0747, E0774;

Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/META-INF/rust-core.xml
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,11 @@
enabledByDefault="true" level="ERROR"
implementationClass="org.rust.ide.inspections.RsWrongGenericParametersNumberInspection"/>

<localInspection language="Rust" groupName="Rust"
displayName="Assignment in a match guard"
enabledByDefault="true" level="ERROR"
implementationClass="org.rust.ide.inspections.RsAssignInMatchGuardInspection"/>

<localInspection language="Rust" groupName="Rust"
displayName="Re-assign immutable variable"
enabledByDefault="true" level="ERROR"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>
<body>
Detects assignment of the matched value in a branch guard.

Corresponds to <a href="https://doc.rust-lang.org/error-index.html#E0510">E0510</a> Rust error.
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Use of this source code is governed by the MIT license that can be
* found in the LICENSE file.
*/

package org.rust.ide.inspections

class RsAssignInMatchGuardInspectionTest : RsInspectionsTestBase(RsAssignInMatchGuardInspection::class) {
fun `test E0510 assign to matched value simple`() = checkByText(
"""
fn main() {
let mut x = Some(0);
match x {
None => {}
Some(_) if {
<error descr="The matched value was assigned in a match guard [E0510]">x = None</error>;
false
} => {}
Some(_) => {}
}
}
"""
)

fun `test E0510 with dereference`() = checkByText(
"""
fn main() {
let mut x = &Some(0);
match *x {
None => (),
Some(_) if {
<error descr="The matched value was assigned in a match guard [E0510]">x = &None</error>;
false
} => (),
Some(_) => (),
}
}
"""
)

fun `test E0510 with lambda call`() = checkByText(
"""
fn main() {
let mut x = &mut &Some(&0);
match **x {
None => {}
Some(&_) if {
(|| {
<error descr="The matched value was assigned in a match guard [E0510]">x = &None</error>;
})();
} => {}
_ => {},
}
}
"""
)

fun `test E0510 with nested block`() = checkByText(
"""
fn main() {
let mut x = &Some(0);
match *x {
None => (),
Some(_) if {
{
<error descr="The matched value was assigned in a match guard [E0510]">x = &None</error>;
}
false
} => (),
Some(_) => (),
}
}
"""
)
}