about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2025-01-06 19:03:55 +0100
committerGuillaume Gomez <guillaume1.gomez@gmail.com>2025-01-07 15:35:21 +0100
commit17f9344a9689d57813d56ee35d95c97f05e6c5e0 (patch)
treea5fc726874c60c952c536457098dbead2542dd2a
parenta9c0e22dfaaaf97e3e589310459fc8c1775c1ab4 (diff)
downloadrust-17f9344a9689d57813d56ee35d95c97f05e6c5e0.tar.gz
rust-17f9344a9689d57813d56ee35d95c97f05e6c5e0.zip
Fix `literal_string_with_formatting_args` lint emitted when it should not
-rw-r--r--clippy_lints/src/literal_string_with_formatting_args.rs2
-rw-r--r--tests/ui/literal_string_with_formatting_arg.rs19
-rw-r--r--tests/ui/literal_string_with_formatting_arg.stderr30
3 files changed, 38 insertions, 13 deletions
diff --git a/clippy_lints/src/literal_string_with_formatting_args.rs b/clippy_lints/src/literal_string_with_formatting_args.rs
index 49353a1b76b..4c8063ee6e6 100644
--- a/clippy_lints/src/literal_string_with_formatting_args.rs
+++ b/clippy_lints/src/literal_string_with_formatting_args.rs
@@ -81,7 +81,7 @@ fn emit_lint(cx: &LateContext<'_>, expr: &Expr<'_>, spans: &[(Span, Option<Strin
 
 impl LateLintPass<'_> for LiteralStringWithFormattingArg {
     fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
-        if expr.span.from_expansion() {
+        if expr.span.from_expansion() || expr.span.is_dummy() {
             return;
         }
         if let ExprKind::Lit(lit) = expr.kind {
diff --git a/tests/ui/literal_string_with_formatting_arg.rs b/tests/ui/literal_string_with_formatting_arg.rs
index f257c66f59d..c9731e592e5 100644
--- a/tests/ui/literal_string_with_formatting_arg.rs
+++ b/tests/ui/literal_string_with_formatting_arg.rs
@@ -1,6 +1,24 @@
 #![warn(clippy::literal_string_with_formatting_args)]
 #![allow(clippy::unnecessary_literal_unwrap)]
 
+// Regression test for <https://github.com/rust-lang/rust-clippy/issues/13885>.
+// It's not supposed to emit the lint in this case (in `assert!` expansion).
+fn compiler_macro() {
+    fn parse(_: &str) -> Result<(), i32> {
+        unimplemented!()
+    }
+
+    assert!(
+        parse(
+            #[allow(clippy::literal_string_with_formatting_args)]
+            "foo {:}"
+        )
+        .is_err()
+    );
+    let value = 0;
+    assert!(format!("{value}").is_ascii());
+}
+
 fn main() {
     let x: Option<usize> = None;
     let y = "hello";
@@ -13,6 +31,7 @@ fn main() {
     x.expect(r"{y:?}  {y:?} "); //~ literal_string_with_formatting_args
     x.expect(r"{y:?} y:?}"); //~ literal_string_with_formatting_args
     x.expect(r##" {y:?} {y:?} "##); //~ literal_string_with_formatting_args
+    assert!("{y}".is_ascii()); //~ literal_string_with_formatting_args
     // Ensure that it doesn't try to go in the middle of a unicode character.
     x.expect("———{:?}"); //~ literal_string_with_formatting_args
 
diff --git a/tests/ui/literal_string_with_formatting_arg.stderr b/tests/ui/literal_string_with_formatting_arg.stderr
index 32a84f600da..1898f4f57b8 100644
--- a/tests/ui/literal_string_with_formatting_arg.stderr
+++ b/tests/ui/literal_string_with_formatting_arg.stderr
@@ -1,5 +1,5 @@
 error: this looks like a formatting argument but it is not part of a formatting macro
-  --> tests/ui/literal_string_with_formatting_arg.rs:7:15
+  --> tests/ui/literal_string_with_formatting_arg.rs:25:15
    |
 LL |     x.expect("{y} {}");
    |               ^^^
@@ -8,64 +8,70 @@ LL |     x.expect("{y} {}");
    = help: to override `-D warnings` add `#[allow(clippy::literal_string_with_formatting_args)]`
 
 error: this looks like a formatting argument but it is not part of a formatting macro
-  --> tests/ui/literal_string_with_formatting_arg.rs:8:16
+  --> tests/ui/literal_string_with_formatting_arg.rs:26:16
    |
 LL |     x.expect(" {y} bla");
    |                ^^^
 
 error: this looks like a formatting argument but it is not part of a formatting macro
-  --> tests/ui/literal_string_with_formatting_arg.rs:9:15
+  --> tests/ui/literal_string_with_formatting_arg.rs:27:15
    |
 LL |     x.expect("{:?}");
    |               ^^^^
 
 error: this looks like a formatting argument but it is not part of a formatting macro
-  --> tests/ui/literal_string_with_formatting_arg.rs:10:15
+  --> tests/ui/literal_string_with_formatting_arg.rs:28:15
    |
 LL |     x.expect("{y:?}");
    |               ^^^^^
 
 error: these look like formatting arguments but are not part of a formatting macro
-  --> tests/ui/literal_string_with_formatting_arg.rs:11:16
+  --> tests/ui/literal_string_with_formatting_arg.rs:29:16
    |
 LL |     x.expect(" {y:?} {y:?} ");
    |                ^^^^^ ^^^^^
 
 error: this looks like a formatting argument but it is not part of a formatting macro
-  --> tests/ui/literal_string_with_formatting_arg.rs:12:23
+  --> tests/ui/literal_string_with_formatting_arg.rs:30:23
    |
 LL |     x.expect(" {y:..} {y:?} ");
    |                       ^^^^^
 
 error: these look like formatting arguments but are not part of a formatting macro
-  --> tests/ui/literal_string_with_formatting_arg.rs:13:16
+  --> tests/ui/literal_string_with_formatting_arg.rs:31:16
    |
 LL |     x.expect(r"{y:?}  {y:?} ");
    |                ^^^^^  ^^^^^
 
 error: this looks like a formatting argument but it is not part of a formatting macro
-  --> tests/ui/literal_string_with_formatting_arg.rs:14:16
+  --> tests/ui/literal_string_with_formatting_arg.rs:32:16
    |
 LL |     x.expect(r"{y:?} y:?}");
    |                ^^^^^
 
 error: these look like formatting arguments but are not part of a formatting macro
-  --> tests/ui/literal_string_with_formatting_arg.rs:15:19
+  --> tests/ui/literal_string_with_formatting_arg.rs:33:19
    |
 LL |     x.expect(r##" {y:?} {y:?} "##);
    |                   ^^^^^ ^^^^^
 
 error: this looks like a formatting argument but it is not part of a formatting macro
-  --> tests/ui/literal_string_with_formatting_arg.rs:17:18
+  --> tests/ui/literal_string_with_formatting_arg.rs:34:14
+   |
+LL |     assert!("{y}".is_ascii());
+   |              ^^^
+
+error: this looks like a formatting argument but it is not part of a formatting macro
+  --> tests/ui/literal_string_with_formatting_arg.rs:36:18
    |
 LL |     x.expect("———{:?}");
    |                  ^^^^
 
 error: this looks like a formatting argument but it is not part of a formatting macro
-  --> tests/ui/literal_string_with_formatting_arg.rs:27:19
+  --> tests/ui/literal_string_with_formatting_arg.rs:46:19
    |
 LL |     x.expect(r##" {x:?} "##); // `x` doesn't exist so we shoud not lint
    |                   ^^^^^
 
-error: aborting due to 11 previous errors
+error: aborting due to 12 previous errors