about summary refs log tree commit diff
path: root/tests/ui/parser
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2024-11-16 00:07:58 +0000
committerEsteban Küber <esteban@kuber.com.ar>2024-12-21 02:46:33 +0000
commit1ce0fa98c7766dc9e168b92ff9bc4e0df4fcaaef (patch)
treeae57bf649227522701a7f1bea6f1844fa77ffec5 /tests/ui/parser
parent9e136a30a965bf4e63f03095c57df7257bf96fd6 (diff)
downloadrust-1ce0fa98c7766dc9e168b92ff9bc4e0df4fcaaef.tar.gz
rust-1ce0fa98c7766dc9e168b92ff9bc4e0df4fcaaef.zip
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/ui/parser')
-rw-r--r--tests/ui/parser/raw/raw-literal-keywords.stderr10
-rw-r--r--tests/ui/parser/recover/missing-dot-on-statement-expression.fixed28
-rw-r--r--tests/ui/parser/recover/missing-dot-on-statement-expression.rs28
-rw-r--r--tests/ui/parser/recover/missing-dot-on-statement-expression.stderr46
4 files changed, 112 insertions, 0 deletions
diff --git a/tests/ui/parser/raw/raw-literal-keywords.stderr b/tests/ui/parser/raw/raw-literal-keywords.stderr
index f7b6c894a90..c7d63672661 100644
--- a/tests/ui/parser/raw/raw-literal-keywords.stderr
+++ b/tests/ui/parser/raw/raw-literal-keywords.stderr
@@ -9,12 +9,22 @@ error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found
    |
 LL |     r#struct Test;
    |              ^^^^ expected one of 8 possible tokens
+   |
+help: you might have meant to write a field access
+   |
+LL |     r#struct.Test;
+   |             +
 
 error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `Test`
   --> $DIR/raw-literal-keywords.rs:10:13
    |
 LL |     r#union Test;
    |             ^^^^ expected one of 8 possible tokens
+   |
+help: you might have meant to write a field access
+   |
+LL |     r#union.Test;
+   |            +
 
 error[E0425]: cannot find value `r#if` in this scope
   --> $DIR/raw-literal-keywords.rs:14:13
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
+