]> git.lizzy.rs Git - rust.git/commitdiff
Re-enable `uninlined_format_args` on multiline `format!`
authorkraktus <kraktus@users.noreply.github.com>
Fri, 25 Nov 2022 15:36:22 +0000 (16:36 +0100)
committerkraktus <kraktus@users.noreply.github.com>
Fri, 25 Nov 2022 15:36:22 +0000 (16:36 +0100)
But do not display the code suggestion which can be sometimes completely broken (fortunately when applied it's valid)

clippy_lints/src/format_args.rs
tests/ui/uninlined_format_args.fixed
tests/ui/uninlined_format_args.stderr

index ec45be558f14baba1aba8dc0e5166a1d268cc8e6..fd3ce2f3d6cd6ab503011cfe479a104abcb63a86 100644 (file)
@@ -9,7 +9,10 @@
 use clippy_utils::ty::{implements_trait, is_type_diagnostic_item};
 use if_chain::if_chain;
 use itertools::Itertools;
-use rustc_errors::Applicability;
+use rustc_errors::{
+    Applicability,
+    SuggestionStyle::{CompletelyHidden, ShowCode},
+};
 use rustc_hir::{Expr, ExprKind, HirId, QPath};
 use rustc_lint::{LateContext, LateLintPass, LintContext};
 use rustc_middle::ty::adjustment::{Adjust, Adjustment};
@@ -286,10 +289,9 @@ fn check_uninlined_args(cx: &LateContext<'_>, args: &FormatArgsExpn<'_>, call_si
         return;
     }
 
-    // Temporarily ignore multiline spans: https://github.com/rust-lang/rust/pull/102729#discussion_r988704308
-    if fixes.iter().any(|(span, _)| cx.sess().source_map().is_multiline(*span)) {
-        return;
-    }
+    // multiline span display suggestion is sometimes broken: https://github.com/rust-lang/rust/pull/102729#discussion_r988704308
+    // in those cases, make the code suggestion hidden
+    let multiline_fix = fixes.iter().any(|(span, _)| cx.sess().source_map().is_multiline(*span));
 
     span_lint_and_then(
         cx,
@@ -297,7 +299,12 @@ fn check_uninlined_args(cx: &LateContext<'_>, args: &FormatArgsExpn<'_>, call_si
         call_site,
         "variables can be used directly in the `format!` string",
         |diag| {
-            diag.multipart_suggestion("change this to", fixes, Applicability::MachineApplicable);
+            diag.multipart_suggestion_with_style(
+                "change this to",
+                fixes,
+                Applicability::MachineApplicable,
+                if multiline_fix { CompletelyHidden } else { ShowCode },
+            );
         },
     );
 }
index 106274479751d7439a50bc2411b9106317447f3b..ca56c95c23f403f354212cf3dc789934b677de65 100644 (file)
@@ -44,9 +44,7 @@ fn tester(fn_arg: i32) {
     println!("val='{local_i32}'"); // space+tab
     println!("val='{local_i32}'"); // tab+space
     println!(
-        "val='{
-    }'",
-        local_i32
+        "val='{local_i32}'"
     );
     println!("{local_i32}");
     println!("{fn_arg}");
@@ -110,8 +108,7 @@ fn tester(fn_arg: i32) {
     println!("{local_f64:width$.prec$}");
     println!("{local_f64:width$.prec$} {local_f64} {width} {prec}");
     println!(
-        "{0:1$.2$} {0:2$.1$} {1:0$.2$} {1:2$.0$} {2:0$.1$} {2:1$.0$}",
-        local_i32, width, prec,
+        "{local_i32:width$.prec$} {local_i32:prec$.width$} {width:local_i32$.prec$} {width:prec$.local_i32$} {prec:local_i32$.width$} {prec:width$.local_i32$}",
     );
     println!(
         "{0:1$.2$} {0:2$.1$} {1:0$.2$} {1:2$.0$} {2:0$.1$} {2:1$.0$} {3}",
@@ -142,9 +139,7 @@ fn tester(fn_arg: i32) {
     println!(no_param_str!(), local_i32);
 
     println!(
-        "{}",
-        // comment with a comma , in it
-        val,
+        "{val}",
     );
     println!("{val}");
 
index 2ce3b7fa960c6159e3c3a237d43220aa195f1516..1182d57ce9b7e066d0275e808d04d2364e08feaa 100644 (file)
@@ -59,6 +59,16 @@ LL -     println!("val='{     }'", local_i32); // tab+space
 LL +     println!("val='{local_i32}'"); // tab+space
    |
 
+error: variables can be used directly in the `format!` string
+  --> $DIR/uninlined_format_args.rs:46:5
+   |
+LL | /     println!(
+LL | |         "val='{
+LL | |     }'",
+LL | |         local_i32
+LL | |     );
+   | |_____^
+
 error: variables can be used directly in the `format!` string
   --> $DIR/uninlined_format_args.rs:51:5
    |
@@ -767,6 +777,15 @@ LL -     println!("{:1$.2$} {0} {1} {2}", local_f64, width, prec);
 LL +     println!("{local_f64:width$.prec$} {local_f64} {width} {prec}");
    |
 
+error: variables can be used directly in the `format!` string
+  --> $DIR/uninlined_format_args.rs:112:5
+   |
+LL | /     println!(
+LL | |         "{0:1$.2$} {0:2$.1$} {1:0$.2$} {1:2$.0$} {2:0$.1$} {2:1$.0$}",
+LL | |         local_i32, width, prec,
+LL | |     );
+   | |_____^
+
 error: variables can be used directly in the `format!` string
   --> $DIR/uninlined_format_args.rs:123:5
    |
@@ -815,6 +834,16 @@ LL -     println!("{}", format!("{}", local_i32));
 LL +     println!("{}", format!("{local_i32}"));
    |
 
+error: variables can be used directly in the `format!` string
+  --> $DIR/uninlined_format_args.rs:144:5
+   |
+LL | /     println!(
+LL | |         "{}",
+LL | |         // comment with a comma , in it
+LL | |         val,
+LL | |     );
+   | |_____^
+
 error: variables can be used directly in the `format!` string
   --> $DIR/uninlined_format_args.rs:149:5
    |
@@ -875,5 +904,5 @@ LL -     println!("expand='{}'", local_i32);
 LL +     println!("expand='{local_i32}'");
    |
 
-error: aborting due to 73 previous errors
+error: aborting due to 76 previous errors