]> git.lizzy.rs Git - rust.git/commitdiff
PR fixing wrong order of format parameters in strings. Issue #106572
authorMads Ravn <madsravn@gmail.com>
Sun, 8 Jan 2023 22:48:41 +0000 (23:48 +0100)
committerMads Ravn <madsravn@gmail.com>
Thu, 2 Feb 2023 11:56:04 +0000 (12:56 +0100)
Adding

Adding

Fixing small issues for PR

Adding tests

Removing unused binding

Changing the wording on note

Fixing PR comment

compiler/rustc_parse_format/src/lib.rs
tests/ui/fmt/format-string-wrong-order.rs [new file with mode: 0644]
tests/ui/fmt/format-string-wrong-order.stderr [new file with mode: 0644]

index 1eb227503f24236971e95f3f978f19054b7d9796..099cbd31917f95eb0a0f87f667c41f022e73843e 100644 (file)
@@ -271,7 +271,13 @@ fn next(&mut self) -> Option<Piece<'a>> {
                                 );
                             }
                         } else {
-                            self.suggest_positional_arg_instead_of_captured_arg(arg);
+                            if let Some(&(_, maybe)) = self.cur.peek() {
+                                if maybe == '?' {
+                                    self.suggest_format();
+                                } else {
+                                    self.suggest_positional_arg_instead_of_captured_arg(arg);
+                                }
+                            }
                         }
                         Some(NextArgument(Box::new(arg)))
                     }
@@ -823,6 +829,27 @@ fn integer(&mut self) -> Option<usize> {
         if found { Some(cur) } else { None }
     }
 
+    fn suggest_format(&mut self) {
+        if let (Some(pos), Some(_)) = (self.consume_pos('?'), self.consume_pos(':')) {
+            let word = self.word();
+            let _end = self.current_pos();
+            let pos = self.to_span_index(pos);
+            self.errors.insert(
+                0,
+                ParseError {
+                    description: "expected format parameter to occur after `:`".to_owned(),
+                    note: Some(
+                        format!("`?` comes after `:`, try `{}:{}` instead", word, "?").to_owned(),
+                    ),
+                    label: "expected `?` to occur after `:`".to_owned(),
+                    span: pos.to(pos),
+                    secondary_label: None,
+                    should_be_replaced_with_positional_argument: false,
+                },
+            );
+        }
+    }
+
     fn suggest_positional_arg_instead_of_captured_arg(&mut self, arg: Argument<'a>) {
         if let Some(end) = self.consume_pos('.') {
             let byte_pos = self.to_span_index(end);
diff --git a/tests/ui/fmt/format-string-wrong-order.rs b/tests/ui/fmt/format-string-wrong-order.rs
new file mode 100644 (file)
index 0000000..0bad540
--- /dev/null
@@ -0,0 +1,15 @@
+fn main() {
+    let bar = 3;
+    format!("{?:}", bar);
+    //~^ ERROR invalid format string: expected format parameter to occur after `:`
+    format!("{?:bar}");
+    //~^ ERROR invalid format string: expected format parameter to occur after `:`
+    format!("{?:?}", bar);
+    //~^ ERROR invalid format string: expected format parameter to occur after `:`
+    format!("{??}", bar);
+    //~^ ERROR invalid format string: expected `'}'`, found `'?'`
+    format!("{?;bar}");
+    //~^ ERROR invalid format string: expected `'}'`, found `'?'`
+    format!("{?:#?}", bar);
+    //~^ ERROR invalid format string: expected format parameter to occur after `:`
+}
diff --git a/tests/ui/fmt/format-string-wrong-order.stderr b/tests/ui/fmt/format-string-wrong-order.stderr
new file mode 100644 (file)
index 0000000..461af35
--- /dev/null
@@ -0,0 +1,54 @@
+error: invalid format string: expected format parameter to occur after `:`
+  --> $DIR/format-string-wrong-order.rs:3:15
+   |
+LL |     format!("{?:}", bar);
+   |               ^ expected `?` to occur after `:` in format string
+   |
+   = note: `?` comes after `:`, try `:?` instead
+
+error: invalid format string: expected format parameter to occur after `:`
+  --> $DIR/format-string-wrong-order.rs:5:15
+   |
+LL |     format!("{?:bar}");
+   |               ^ expected `?` to occur after `:` in format string
+   |
+   = note: `?` comes after `:`, try `bar:?` instead
+
+error: invalid format string: expected format parameter to occur after `:`
+  --> $DIR/format-string-wrong-order.rs:7:15
+   |
+LL |     format!("{?:?}", bar);
+   |               ^ expected `?` to occur after `:` in format string
+   |
+   = note: `?` comes after `:`, try `:?` instead
+
+error: invalid format string: expected `'}'`, found `'?'`
+  --> $DIR/format-string-wrong-order.rs:9:15
+   |
+LL |     format!("{??}", bar);
+   |              -^ expected `}` in format string
+   |              |
+   |              because of this opening brace
+   |
+   = note: if you intended to print `{`, you can escape it using `{{`
+
+error: invalid format string: expected `'}'`, found `'?'`
+  --> $DIR/format-string-wrong-order.rs:11:15
+   |
+LL |     format!("{?;bar}");
+   |              -^ expected `}` in format string
+   |              |
+   |              because of this opening brace
+   |
+   = note: if you intended to print `{`, you can escape it using `{{`
+
+error: invalid format string: expected format parameter to occur after `:`
+  --> $DIR/format-string-wrong-order.rs:13:15
+   |
+LL |     format!("{?:#?}", bar);
+   |               ^ expected `?` to occur after `:` in format string
+   |
+   = note: `?` comes after `:`, try `:?` instead
+
+error: aborting due to 6 previous errors
+