Skip to content

Commit

Permalink
#5215: Get rid of FillMatchArmsIntention
Browse files Browse the repository at this point in the history
Empty match expressions filling is now implemented
in `RsNonExhaustiveMatchInspection`
  • Loading branch information
artemmukhin committed Sep 11, 2020
1 parent 3ec2a30 commit 3c31a2a
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 364 deletions.
64 changes: 0 additions & 64 deletions src/main/kotlin/org/rust/ide/intentions/FillMatchArmsIntention.kt

This file was deleted.

18 changes: 0 additions & 18 deletions src/main/kotlin/org/rust/lang/core/psi/RsPsiFactory.kt
Expand Up @@ -17,7 +17,6 @@ import org.rust.lang.core.macros.prepareExpandedTextForParsing
import org.rust.lang.core.parser.RustParserUtil.PathParsingMode
import org.rust.lang.core.parser.RustParserUtil.PathParsingMode.*
import org.rust.lang.core.psi.ext.*
import org.rust.lang.core.resolve.VALUES
import org.rust.lang.core.types.Substitution
import org.rust.lang.core.types.infer.substitute
import org.rust.lang.core.types.ty.Mutability
Expand Down Expand Up @@ -314,23 +313,6 @@ class RsPsiFactory(
createFromText("#![$text]")
?: error("Failed to create an inner attribute from text: `$text`")

fun createMatchBody(context: RsElement, enumName: String, variants: List<RsEnumVariant>): RsMatchBody {
val matchBodyText = variants.joinToString(",\n", postfix = ",") { variant ->
val variantName = variant.name ?: return@joinToString ""
val tupleFields = variant.tupleFields?.tupleFieldDeclList
val blockFields = variant.blockFields
val suffix = when {
tupleFields != null -> tupleFields.joinToString(", ", " (", ")") { "_" }
blockFields != null -> " { .. }"
else -> ""
}
val prefix = if (context.findInScope(variantName, VALUES) != variant) "$enumName::" else ""
"$prefix$variantName$suffix => {}"
}
return createExpressionOfType<RsMatchExpr>("match x { $matchBodyText }").matchBody
?: error("Failed to create match body from text: `$matchBodyText`")
}

fun createMatchBody(patterns: List<Pattern>, ctx: RsElement? = null): RsMatchBody {
val arms = patterns.joinToString("\n") { "${it.text(ctx)} => {}" }
return createExpressionOfType<RsMatchExpr>("match x { $arms }").matchBody
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Expand Up @@ -7,6 +7,7 @@ package org.rust.ide.inspections.match

import org.rust.ProjectDescriptor
import org.rust.WithDependencyRustProjectDescriptor
import org.rust.WithStdlibRustProjectDescriptor
import org.rust.ide.inspections.RsInspectionsTestBase
import org.rust.ide.inspections.checkMatch.RsNonExhaustiveMatchInspection

Expand Down Expand Up @@ -765,4 +766,134 @@ class RsNonExhaustiveMatchInspectionTest : RsInspectionsTestBase(RsNonExhaustive
}
}
""")

fun `test empty match different enum variants`() = checkFixByText("Add remaining patterns", """
enum Foo {
X,
Y(i32),
Z { foo: bool }
}
fn foo(x: Foo) {
<error descr="Match must be exhaustive [E0004]">match/*caret*/</error> x {}
}
""", """
enum Foo {
X,
Y(i32),
Z { foo: bool }
}
fn foo(x: Foo) {
match/*caret*/ x {
Foo::X => {}
Foo::Y(_) => {}
Foo::Z { .. } => {}
}
}
""")

fun `test empty match don't remove comments`() = checkFixByText("Add remaining patterns", """
enum FooBar {
Foo,
Bar
}
fn foo(x: FooBar) {
<error descr="Match must be exhaustive [E0004]">match/*caret*/</error> x {
// test
}
}
""", """
enum FooBar {
Foo,
Bar
}
fn foo(x: FooBar) {
match/*caret*/ x {
// test
FooBar::Foo => {}
FooBar::Bar => {}
}
}
""")

@ProjectDescriptor(WithStdlibRustProjectDescriptor::class)
fun `test empty match Option enum`() = checkFixByText("Add remaining patterns", """
fn foo(x: Option<i32>) {
<error descr="Match must be exhaustive [E0004]">match/*caret*/</error> x {}
}
""", """
fn foo(x: Option<i32>) {
match/*caret*/ x {
None => {}
Some(_) => {}
}
}
""")

@ProjectDescriptor(WithStdlibRustProjectDescriptor::class)
fun `test empty match Result enum`() = checkFixByText("Add remaining patterns", """
fn foo(x: Result<i32, bool>) {
<error descr="Match must be exhaustive [E0004]">match/*caret*/</error> x {}
}
""", """
fn foo(x: Result<i32, bool>) {
match/*caret*/ x {
Ok(_) => {}
Err(_) => {}
}
}
""")

fun `test empty match one variant is in scope`() = checkFixByText("Add remaining patterns", """
mod foo {
enum E { A, B }
}
use foo::E;
use foo::E::A;
fn foo(x: &E) {
<error descr="Match must be exhaustive [E0004]">match/*caret*/</error> x {}
}
""", """
mod foo {
enum E { A, B }
}
use foo::E;
use foo::E::A;
fn foo(x: &E) {
match/*caret*/ x {
A => {}
E::B => {}
}
}
""")

fun `test empty match import unresolved type`() = checkFixByText("Add remaining patterns", """
use a::foo;
mod a {
pub enum FooBar { Foo, Bar }
pub fn foo() -> FooBar { FooBar::Foo }
}
fn main() {
<error descr="Match must be exhaustive [E0004]">match/*caret*/</error> foo() {};
}
""", """
use a::{foo, FooBar};
mod a {
pub enum FooBar { Foo, Bar }
pub fn foo() -> FooBar { FooBar::Foo }
}
fn main() {
match/*caret*/ foo() {
FooBar::Foo => {}
FooBar::Bar => {}
};
}
""")
}

0 comments on commit 3c31a2a

Please sign in to comment.