about summary refs log tree commit diff
diff options
context:
space:
mode:
authormcarton <cartonmartin+git@gmail.com>2018-10-02 23:55:25 +0200
committermcarton <cartonmartin+git@gmail.com>2018-10-02 23:57:22 +0200
commit7eebd5b20c215c4baa98a1ac569e543712451c33 (patch)
tree4cd88805d7540e9ed3919fa2b5e0dd089a4a5e69
parentd18c7b272245f41cea9303ca62beb9ca64615251 (diff)
downloadrust-7eebd5b20c215c4baa98a1ac569e543712451c33.tar.gz
rust-7eebd5b20c215c4baa98a1ac569e543712451c33.zip
Ignore `format!` with precision in `USELESS_FORMAT`
-rw-r--r--clippy_lints/src/format.rs10
-rw-r--r--tests/ui/format.rs6
2 files changed, 12 insertions, 4 deletions
diff --git a/clippy_lints/src/format.rs b/clippy_lints/src/format.rs
index 2eb95ebffbf..2d40150bc8e 100644
--- a/clippy_lints/src/format.rs
+++ b/clippy_lints/src/format.rs
@@ -47,7 +47,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
                 return;
             }
             match expr.node {
-
                 // `format!("{}", foo)` expansion
                 ExprKind::Call(ref fun, ref args) => {
                     if_chain! {
@@ -162,9 +161,12 @@ fn check_unformatted(expr: &Expr) -> bool {
         if let ExprKind::Struct(_, ref fields, _) = exprs[0].node;
         if let Some(format_field) = fields.iter().find(|f| f.ident.name == "format");
         if let ExprKind::Struct(_, ref fields, _) = format_field.expr.node;
-        if let Some(align_field) = fields.iter().find(|f| f.ident.name == "width");
-        if let ExprKind::Path(ref qpath) = align_field.expr.node;
-        if last_path_segment(qpath).ident.name == "Implied";
+        if let Some(width_field) = fields.iter().find(|f| f.ident.name == "width");
+        if let ExprKind::Path(ref width_qpath) = width_field.expr.node;
+        if last_path_segment(width_qpath).ident.name == "Implied";
+        if let Some(precision_field) = fields.iter().find(|f| f.ident.name == "precision");
+        if let ExprKind::Path(ref precision_path) = precision_field.expr.node;
+        if last_path_segment(precision_path).ident.name == "Implied";
         then {
             return true;
         }
diff --git a/tests/ui/format.rs b/tests/ui/format.rs
index 1b467dae0cc..0162f34c085 100644
--- a/tests/ui/format.rs
+++ b/tests/ui/format.rs
@@ -46,4 +46,10 @@ fn main() {
 
     // A format! inside a macro should not trigger a warning
     foo!("should not warn");
+
+    // precision on string means slicing without panicking on size:
+    format!("{:.1}", "foo"); // could be "foo"[..1]
+    format!("{:.10}", "foo"); // could not be "foo"[..10]
+    format!("{:.prec$}", "foo", prec = 1);
+    format!("{:.prec$}", "foo", prec = 10);
 }