Skip to content
Permalink
Browse files

Add inspection for anonymous parameters.

  • Loading branch information...
matklad committed Jul 19, 2016
1 parent 1bb65c4 commit 28c5b944263364b2c5f926f8ccb03b16d31774da
@@ -0,0 +1,38 @@
package org.rust.ide.inspections

import com.intellij.codeInspection.LocalQuickFix
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.codeInspection.ProblemHighlightType
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElementVisitor
import org.rust.lang.core.psi.RustElementFactory
import org.rust.lang.core.psi.RustElementVisitor
import org.rust.lang.core.psi.RustParameterElement
import org.rust.lang.core.psi.RustTraitMethodMemberElement

class RustAnonParamInspection : RustLocalInspectionTool() {

override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
return object : RustElementVisitor() {
override fun visitTraitMethodMember(o: RustTraitMethodMemberElement) {
for (param in o.parameters?.parameterList.orEmpty()) {
if (param.pat == null) {
holder.registerProblem(param, "Anonymous trait method parameter", object : LocalQuickFix {
override fun getName(): String = "Add parameter pattern"

override fun getFamilyName(): String = name

override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val p = descriptor.startElement as RustParameterElement
check(p.pat == null && p.type != null)
p.replace(RustElementFactory.paramWithPattern(project, p))
}
})
}
}
}
}
}
}

@@ -53,4 +53,7 @@ object RustElementFactory {
val where = whereClause?.text ?: ""
return "fn $name $generics (${allArguments.joinToString(",")}) $ret $where"
}

fun paramWithPattern(project: Project, param: RustParameterElement): RustParameterElement =
createFromText(project, "trait T { fn foo(_: ${param.text}); }")!!
}
@@ -117,6 +117,11 @@

<!-- Inspections -->

<localInspection language="Rust" groupName="Rust"
displayName="Anonymous parameters"
enabledByDefault="true" level="WARNING"
implementationClass="org.rust.ide.inspections.RustAnonParamInspection"/>

<localInspection language="Rust" groupName="Rust"
displayName="Approximate Constants"
enabledByDefault="true" level="WARNING"
@@ -6,4 +6,11 @@ class RustInspectionsTest : RustInspectionsTestBase() {

fun testApproxConstant() = doTest<RustApproxConstantInspection>()
fun testSelfConvention() = doTest<RustSelfConventionInspection>()

fun testAnonParam() = doTest<RustAnonParamInspection>()

fun testAnonParamFix() = checkByFile {
enableInspection<RustAnonParamInspection>()
applyQuickFix("Add parameter pattern")
}
}
@@ -0,0 +1,3 @@
trait T {
fn foo(<warning>i32</warning>, <warning>String</warning>);
}
@@ -0,0 +1,3 @@
trait T {
fn foo(<caret>i32);
}
@@ -0,0 +1,3 @@
trait T {
fn foo(_: i32);
}

0 comments on commit 28c5b94

Please sign in to comment.
You can’t perform that action at this time.