diff options
| author | Jacob Pratt <jacob@jhpratt.dev> | 2024-12-21 01:18:40 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-12-21 01:18:40 -0500 |
| commit | 36485acdac216c45fcdf8cacd5b762ffe2d58af7 (patch) | |
| tree | d999f8a2664d8e106514d3f64410870c0f050d98 /tests | |
| parent | 13170cd787cb733ed24842ee825bcbd98dc01476 (diff) | |
| parent | 1549af29c3fea4c9afde42712b5092d8cc98e140 (diff) | |
| download | rust-36485acdac216c45fcdf8cacd5b762ffe2d58af7.tar.gz rust-36485acdac216c45fcdf8cacd5b762ffe2d58af7.zip | |
Rollup merge of #133087 - estebank:stmt-misparse, r=chenyukang
Detect missing `.` in method chain in `let` bindings and statements On parse errors where an ident is found where one wasn't expected, see if the next elements might have been meant as method call or field access. ``` error: expected one of `.`, `;`, `?`, `else`, or an operator, found `map` --> $DIR/missing-dot-on-statement-expression.rs:7:29 | LL | let _ = [1, 2, 3].iter()map(|x| x); | ^^^ expected one of `.`, `;`, `?`, `else`, or an operator | help: you might have meant to write a method call | LL | let _ = [1, 2, 3].iter().map(|x| x); | + ```
Diffstat (limited to 'tests')
4 files changed, 107 insertions, 0 deletions
diff --git a/tests/ui/parser/recover/missing-dot-on-statement-expression.fixed b/tests/ui/parser/recover/missing-dot-on-statement-expression.fixed new file mode 100644 index 00000000000..1be4485b474 --- /dev/null +++ b/tests/ui/parser/recover/missing-dot-on-statement-expression.fixed @@ -0,0 +1,28 @@ +//@ run-rustfix +#![allow(unused_must_use, dead_code)] +struct S { + field: (), +} +fn main() { + let _ = [1, 2, 3].iter().map(|x| x); //~ ERROR expected one of `.`, `;`, `?`, `else`, or an operator, found `map` + //~^ HELP you might have meant to write a method call +} +fn foo() { + let baz = S { + field: () + }; + let _ = baz.field; //~ ERROR expected one of `!`, `.`, `::`, `;`, `?`, `else`, `{`, or an operator, found `field` + //~^ HELP you might have meant to write a field +} + +fn bar() { + [1, 2, 3].iter().map(|x| x); //~ ERROR expected one of `.`, `;`, `?`, `}`, or an operator, found `map` + //~^ HELP you might have meant to write a method call +} +fn baz() { + let baz = S { + field: () + }; + baz.field; //~ ERROR expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `field` + //~^ HELP you might have meant to write a field +} diff --git a/tests/ui/parser/recover/missing-dot-on-statement-expression.rs b/tests/ui/parser/recover/missing-dot-on-statement-expression.rs new file mode 100644 index 00000000000..5e2b545f414 --- /dev/null +++ b/tests/ui/parser/recover/missing-dot-on-statement-expression.rs @@ -0,0 +1,28 @@ +//@ run-rustfix +#![allow(unused_must_use, dead_code)] +struct S { + field: (), +} +fn main() { + let _ = [1, 2, 3].iter()map(|x| x); //~ ERROR expected one of `.`, `;`, `?`, `else`, or an operator, found `map` + //~^ HELP you might have meant to write a method call +} +fn foo() { + let baz = S { + field: () + }; + let _ = baz field; //~ ERROR expected one of `!`, `.`, `::`, `;`, `?`, `else`, `{`, or an operator, found `field` + //~^ HELP you might have meant to write a field +} + +fn bar() { + [1, 2, 3].iter()map(|x| x); //~ ERROR expected one of `.`, `;`, `?`, `}`, or an operator, found `map` + //~^ HELP you might have meant to write a method call +} +fn baz() { + let baz = S { + field: () + }; + baz field; //~ ERROR expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `field` + //~^ HELP you might have meant to write a field +} diff --git a/tests/ui/parser/recover/missing-dot-on-statement-expression.stderr b/tests/ui/parser/recover/missing-dot-on-statement-expression.stderr new file mode 100644 index 00000000000..a04d8bd34e2 --- /dev/null +++ b/tests/ui/parser/recover/missing-dot-on-statement-expression.stderr @@ -0,0 +1,46 @@ +error: expected one of `.`, `;`, `?`, `else`, or an operator, found `map` + --> $DIR/missing-dot-on-statement-expression.rs:7:29 + | +LL | let _ = [1, 2, 3].iter()map(|x| x); + | ^^^ expected one of `.`, `;`, `?`, `else`, or an operator + | +help: you might have meant to write a method call + | +LL | let _ = [1, 2, 3].iter().map(|x| x); + | + + +error: expected one of `!`, `.`, `::`, `;`, `?`, `else`, `{`, or an operator, found `field` + --> $DIR/missing-dot-on-statement-expression.rs:14:17 + | +LL | let _ = baz field; + | ^^^^^ expected one of 8 possible tokens + | +help: you might have meant to write a field access + | +LL | let _ = baz.field; + | + + +error: expected one of `.`, `;`, `?`, `}`, or an operator, found `map` + --> $DIR/missing-dot-on-statement-expression.rs:19:21 + | +LL | [1, 2, 3].iter()map(|x| x); + | ^^^ expected one of `.`, `;`, `?`, `}`, or an operator + | +help: you might have meant to write a method call + | +LL | [1, 2, 3].iter().map(|x| x); + | + + +error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `field` + --> $DIR/missing-dot-on-statement-expression.rs:26:9 + | +LL | baz field; + | ^^^^^ expected one of 8 possible tokens + | +help: you might have meant to write a field access + | +LL | baz.field; + | + + +error: aborting due to 4 previous errors + diff --git a/tests/ui/suggestions/type-ascription-and-other-error.stderr b/tests/ui/suggestions/type-ascription-and-other-error.stderr index 4efddca4b47..7f8b8b7470e 100644 --- a/tests/ui/suggestions/type-ascription-and-other-error.stderr +++ b/tests/ui/suggestions/type-ascription-and-other-error.stderr @@ -3,6 +3,11 @@ error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found | LL | not rust; | ^^^^ expected one of 8 possible tokens + | +help: you might have meant to write a field access + | +LL | not.rust; + | + error: aborting due to 1 previous error |
