From 415394c3fc0e008026b3f2a37fbd57a58449b4d3 Mon Sep 17 00:00:00 2001 From: pro-grammer1 <1df0d0d3-eed4-45fc-bc60-43a85079f3f9@anonaddy.me> Date: Tue, 1 Dec 2020 09:44:43 +0000 Subject: [PATCH 1/6] Fix false positive in write_literal and print_literal due to numeric literals --- clippy_lints/src/write.rs | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/clippy_lints/src/write.rs b/clippy_lints/src/write.rs index ff414f748ef..78d23e1e0ef 100644 --- a/clippy_lints/src/write.rs +++ b/clippy_lints/src/write.rs @@ -2,7 +2,8 @@ use std::borrow::Cow; use std::ops::Range; use crate::utils::{snippet_with_applicability, span_lint, span_lint_and_sugg, span_lint_and_then}; -use rustc_ast::ast::{Expr, ExprKind, Item, ItemKind, MacCall, StrLit, StrStyle}; +use if_chain::if_chain; +use rustc_ast::ast::{Expr, ExprKind, Item, ItemKind, LitKind, MacCall, StrLit, StrStyle}; use rustc_ast::token; use rustc_ast::tokenstream::TokenStream; use rustc_errors::Applicability; @@ -442,7 +443,12 @@ impl Write { return (Some(fmtstr), None); }; match &token_expr.kind { - ExprKind::Lit(_) => { + ExprKind::Lit(lit) + if match lit.kind { + LitKind::Int(_, _) | LitKind::Float(_, _) => false, + _ => true, + } => + { let mut all_simple = true; let mut seen = false; for arg in &args { @@ -460,10 +466,16 @@ impl Write { span_lint(cx, lint, token_expr.span, "literal with an empty format string"); } idx += 1; - }, + } ExprKind::Assign(lhs, rhs, _) => { - if let ExprKind::Lit(_) = rhs.kind { - if let ExprKind::Path(_, p) = &lhs.kind { + if_chain! { + if let ExprKind::Lit(ref lit) = rhs.kind; + if match lit.kind { + LitKind::Int(_, _) | LitKind::Float(_, _) => false, + _ => true, + }; + if let ExprKind::Path(_, p) = &lhs.kind; + then { let mut all_simple = true; let mut seen = false; for arg in &args { From 0d542b7310294a32023e935916a4d8c8f47ec1a8 Mon Sep 17 00:00:00 2001 From: pro-grammer1 <1df0d0d3-eed4-45fc-bc60-43a85079f3f9@anonaddy.me> Date: Sun, 17 Jan 2021 08:48:37 +0000 Subject: [PATCH 2/6] Run tests/ui/update-all-references.sh and refactor match into matches! --- clippy_lints/src/write.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/clippy_lints/src/write.rs b/clippy_lints/src/write.rs index 78d23e1e0ef..52118a56bb6 100644 --- a/clippy_lints/src/write.rs +++ b/clippy_lints/src/write.rs @@ -470,10 +470,7 @@ impl Write { ExprKind::Assign(lhs, rhs, _) => { if_chain! { if let ExprKind::Lit(ref lit) = rhs.kind; - if match lit.kind { - LitKind::Int(_, _) | LitKind::Float(_, _) => false, - _ => true, - }; + if matches!(lit.kind, LitKind::Int(..) | LitKind::Float(..)); if let ExprKind::Path(_, p) = &lhs.kind; then { let mut all_simple = true; From 2af642da28109c96145c7a5613ae2ec37d359448 Mon Sep 17 00:00:00 2001 From: pro-grammer1 <1df0d0d3-eed4-45fc-bc60-43a85079f3f9@anonaddy.me> Date: Sun, 17 Jan 2021 18:01:01 +0000 Subject: [PATCH 3/6] Replace another instance of match with matches --- clippy_lints/src/write.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/clippy_lints/src/write.rs b/clippy_lints/src/write.rs index 52118a56bb6..6721639f724 100644 --- a/clippy_lints/src/write.rs +++ b/clippy_lints/src/write.rs @@ -443,12 +443,7 @@ impl Write { return (Some(fmtstr), None); }; match &token_expr.kind { - ExprKind::Lit(lit) - if match lit.kind { - LitKind::Int(_, _) | LitKind::Float(_, _) => false, - _ => true, - } => - { + ExprKind::Lit(lit) if matches!(lit.kind, LitKind::Int(..) | LitKind::Float(..)) => { let mut all_simple = true; let mut seen = false; for arg in &args { From ab155b14a27811c29e35bea76bfbf3845bc79fdf Mon Sep 17 00:00:00 2001 From: pro-grammer1 <1df0d0d3-eed4-45fc-bc60-43a85079f3f9@anonaddy.me> Date: Sun, 17 Jan 2021 18:21:58 +0000 Subject: [PATCH 4/6] Negate results of matches! --- clippy_lints/src/write.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/write.rs b/clippy_lints/src/write.rs index 6721639f724..60a9fb59cd2 100644 --- a/clippy_lints/src/write.rs +++ b/clippy_lints/src/write.rs @@ -443,7 +443,7 @@ impl Write { return (Some(fmtstr), None); }; match &token_expr.kind { - ExprKind::Lit(lit) if matches!(lit.kind, LitKind::Int(..) | LitKind::Float(..)) => { + ExprKind::Lit(lit) if !matches!(lit.kind, LitKind::Int(..) | LitKind::Float(..)) => { let mut all_simple = true; let mut seen = false; for arg in &args { @@ -465,7 +465,7 @@ impl Write { ExprKind::Assign(lhs, rhs, _) => { if_chain! { if let ExprKind::Lit(ref lit) = rhs.kind; - if matches!(lit.kind, LitKind::Int(..) | LitKind::Float(..)); + if !matches!(lit.kind, LitKind::Int(..) | LitKind::Float(..)); if let ExprKind::Path(_, p) = &lhs.kind; then { let mut all_simple = true; From fb2a06dcce0680dfe8d2bed542be3c55eb2e18c7 Mon Sep 17 00:00:00 2001 From: pro-grammer1 <1df0d0d3-eed4-45fc-bc60-43a85079f3f9@anonaddy.me> Date: Sun, 17 Jan 2021 18:55:59 +0000 Subject: [PATCH 5/6] Remove numeric literals from print_literal and write_literal tests --- clippy_lints/src/write.rs | 2 +- tests/ui/crashes/ice-3891.stderr | 4 +-- tests/ui/print_literal.rs | 3 --- tests/ui/print_literal.stderr | 46 ++++++++++---------------------- tests/ui/write_literal.rs | 3 --- tests/ui/write_literal.stderr | 46 ++++++++++---------------------- 6 files changed, 31 insertions(+), 73 deletions(-) diff --git a/clippy_lints/src/write.rs b/clippy_lints/src/write.rs index 60a9fb59cd2..503cb82c3e5 100644 --- a/clippy_lints/src/write.rs +++ b/clippy_lints/src/write.rs @@ -461,7 +461,7 @@ impl Write { span_lint(cx, lint, token_expr.span, "literal with an empty format string"); } idx += 1; - } + }, ExprKind::Assign(lhs, rhs, _) => { if_chain! { if let ExprKind::Lit(ref lit) = rhs.kind; diff --git a/tests/ui/crashes/ice-3891.stderr b/tests/ui/crashes/ice-3891.stderr index 5a285b0e714..59469ec5891 100644 --- a/tests/ui/crashes/ice-3891.stderr +++ b/tests/ui/crashes/ice-3891.stderr @@ -1,10 +1,10 @@ -error: invalid suffix `x` for integer literal +error: invalid suffix `x` for number literal --> $DIR/ice-3891.rs:2:5 | LL | 1x; | ^^ invalid suffix `x` | - = help: the suffix must be one of the integral types (`u32`, `isize`, etc) + = help: the suffix must be one of the numeric types (`u32`, `isize`, `f32`, etc.) error: aborting due to previous error diff --git a/tests/ui/print_literal.rs b/tests/ui/print_literal.rs index 40ed18e9302..0c8aecc2d8c 100644 --- a/tests/ui/print_literal.rs +++ b/tests/ui/print_literal.rs @@ -19,12 +19,9 @@ fn main() { println!("{number:>0width$}", number = 1, width = 6); // these should throw warnings - println!("{} of {:b} people know binary, the other half doesn't", 1, 2); print!("Hello {}", "world"); println!("Hello {} {}", world, "world"); println!("Hello {}", "world"); - println!("10 / 4 is {}", 2.5); - println!("2 + 1 = {}", 3); // positional args don't change the fact // that we're using a literal -- this should diff --git a/tests/ui/print_literal.stderr b/tests/ui/print_literal.stderr index fc502e9f71d..692abdb3054 100644 --- a/tests/ui/print_literal.stderr +++ b/tests/ui/print_literal.stderr @@ -1,88 +1,70 @@ error: literal with an empty format string - --> $DIR/print_literal.rs:22:71 + --> $DIR/print_literal.rs:22:24 | -LL | println!("{} of {:b} people know binary, the other half doesn't", 1, 2); - | ^ +LL | print!("Hello {}", "world"); + | ^^^^^^^ | = note: `-D clippy::print-literal` implied by `-D warnings` error: literal with an empty format string - --> $DIR/print_literal.rs:23:24 - | -LL | print!("Hello {}", "world"); - | ^^^^^^^ - -error: literal with an empty format string - --> $DIR/print_literal.rs:24:36 + --> $DIR/print_literal.rs:23:36 | LL | println!("Hello {} {}", world, "world"); | ^^^^^^^ error: literal with an empty format string - --> $DIR/print_literal.rs:25:26 + --> $DIR/print_literal.rs:24:26 | LL | println!("Hello {}", "world"); | ^^^^^^^ error: literal with an empty format string - --> $DIR/print_literal.rs:26:30 - | -LL | println!("10 / 4 is {}", 2.5); - | ^^^ - -error: literal with an empty format string - --> $DIR/print_literal.rs:27:28 - | -LL | println!("2 + 1 = {}", 3); - | ^ - -error: literal with an empty format string - --> $DIR/print_literal.rs:32:25 + --> $DIR/print_literal.rs:29:25 | LL | println!("{0} {1}", "hello", "world"); | ^^^^^^^ error: literal with an empty format string - --> $DIR/print_literal.rs:32:34 + --> $DIR/print_literal.rs:29:34 | LL | println!("{0} {1}", "hello", "world"); | ^^^^^^^ error: literal with an empty format string - --> $DIR/print_literal.rs:33:25 + --> $DIR/print_literal.rs:30:25 | LL | println!("{1} {0}", "hello", "world"); | ^^^^^^^ error: literal with an empty format string - --> $DIR/print_literal.rs:33:34 + --> $DIR/print_literal.rs:30:34 | LL | println!("{1} {0}", "hello", "world"); | ^^^^^^^ error: literal with an empty format string - --> $DIR/print_literal.rs:36:35 + --> $DIR/print_literal.rs:33:35 | LL | println!("{foo} {bar}", foo = "hello", bar = "world"); | ^^^^^^^ error: literal with an empty format string - --> $DIR/print_literal.rs:36:50 + --> $DIR/print_literal.rs:33:50 | LL | println!("{foo} {bar}", foo = "hello", bar = "world"); | ^^^^^^^ error: literal with an empty format string - --> $DIR/print_literal.rs:37:35 + --> $DIR/print_literal.rs:34:35 | LL | println!("{bar} {foo}", foo = "hello", bar = "world"); | ^^^^^^^ error: literal with an empty format string - --> $DIR/print_literal.rs:37:50 + --> $DIR/print_literal.rs:34:50 | LL | println!("{bar} {foo}", foo = "hello", bar = "world"); | ^^^^^^^ -error: aborting due to 14 previous errors +error: aborting due to 11 previous errors diff --git a/tests/ui/write_literal.rs b/tests/ui/write_literal.rs index d8205c5eb67..f5de7ea71d3 100644 --- a/tests/ui/write_literal.rs +++ b/tests/ui/write_literal.rs @@ -24,12 +24,9 @@ fn main() { writeln!(&mut v, "{number:>0width$}", number = 1, width = 6); // these should throw warnings - writeln!(&mut v, "{} of {:b} people know binary, the other half doesn't", 1, 2); write!(&mut v, "Hello {}", "world"); writeln!(&mut v, "Hello {} {}", world, "world"); writeln!(&mut v, "Hello {}", "world"); - writeln!(&mut v, "10 / 4 is {}", 2.5); - writeln!(&mut v, "2 + 1 = {}", 3); // positional args don't change the fact // that we're using a literal -- this should diff --git a/tests/ui/write_literal.stderr b/tests/ui/write_literal.stderr index 54a787fe555..4dcaaa474a8 100644 --- a/tests/ui/write_literal.stderr +++ b/tests/ui/write_literal.stderr @@ -1,88 +1,70 @@ error: literal with an empty format string - --> $DIR/write_literal.rs:27:79 + --> $DIR/write_literal.rs:27:32 | -LL | writeln!(&mut v, "{} of {:b} people know binary, the other half doesn't", 1, 2); - | ^ +LL | write!(&mut v, "Hello {}", "world"); + | ^^^^^^^ | = note: `-D clippy::write-literal` implied by `-D warnings` error: literal with an empty format string - --> $DIR/write_literal.rs:28:32 - | -LL | write!(&mut v, "Hello {}", "world"); - | ^^^^^^^ - -error: literal with an empty format string - --> $DIR/write_literal.rs:29:44 + --> $DIR/write_literal.rs:28:44 | LL | writeln!(&mut v, "Hello {} {}", world, "world"); | ^^^^^^^ error: literal with an empty format string - --> $DIR/write_literal.rs:30:34 + --> $DIR/write_literal.rs:29:34 | LL | writeln!(&mut v, "Hello {}", "world"); | ^^^^^^^ error: literal with an empty format string - --> $DIR/write_literal.rs:31:38 - | -LL | writeln!(&mut v, "10 / 4 is {}", 2.5); - | ^^^ - -error: literal with an empty format string - --> $DIR/write_literal.rs:32:36 - | -LL | writeln!(&mut v, "2 + 1 = {}", 3); - | ^ - -error: literal with an empty format string - --> $DIR/write_literal.rs:37:33 + --> $DIR/write_literal.rs:34:33 | LL | writeln!(&mut v, "{0} {1}", "hello", "world"); | ^^^^^^^ error: literal with an empty format string - --> $DIR/write_literal.rs:37:42 + --> $DIR/write_literal.rs:34:42 | LL | writeln!(&mut v, "{0} {1}", "hello", "world"); | ^^^^^^^ error: literal with an empty format string - --> $DIR/write_literal.rs:38:33 + --> $DIR/write_literal.rs:35:33 | LL | writeln!(&mut v, "{1} {0}", "hello", "world"); | ^^^^^^^ error: literal with an empty format string - --> $DIR/write_literal.rs:38:42 + --> $DIR/write_literal.rs:35:42 | LL | writeln!(&mut v, "{1} {0}", "hello", "world"); | ^^^^^^^ error: literal with an empty format string - --> $DIR/write_literal.rs:41:43 + --> $DIR/write_literal.rs:38:43 | LL | writeln!(&mut v, "{foo} {bar}", foo = "hello", bar = "world"); | ^^^^^^^ error: literal with an empty format string - --> $DIR/write_literal.rs:41:58 + --> $DIR/write_literal.rs:38:58 | LL | writeln!(&mut v, "{foo} {bar}", foo = "hello", bar = "world"); | ^^^^^^^ error: literal with an empty format string - --> $DIR/write_literal.rs:42:43 + --> $DIR/write_literal.rs:39:43 | LL | writeln!(&mut v, "{bar} {foo}", foo = "hello", bar = "world"); | ^^^^^^^ error: literal with an empty format string - --> $DIR/write_literal.rs:42:58 + --> $DIR/write_literal.rs:39:58 | LL | writeln!(&mut v, "{bar} {foo}", foo = "hello", bar = "world"); | ^^^^^^^ -error: aborting due to 14 previous errors +error: aborting due to 11 previous errors From 32b2a3f944f9889fd7c5fcfb7976430d1aeb7ed4 Mon Sep 17 00:00:00 2001 From: pro-grammer1 <1df0d0d3-eed4-45fc-bc60-43a85079f3f9@anonaddy.me> Date: Sun, 17 Jan 2021 19:21:33 +0000 Subject: [PATCH 6/6] Add numeric literals to the write_literal and print_literal tests that shouldn't fail --- tests/ui/print_literal.rs | 3 +++ tests/ui/print_literal.stderr | 22 +++++++++++----------- tests/ui/write_literal.rs | 3 +++ tests/ui/write_literal.stderr | 22 +++++++++++----------- 4 files changed, 28 insertions(+), 22 deletions(-) diff --git a/tests/ui/print_literal.rs b/tests/ui/print_literal.rs index 0c8aecc2d8c..8665a3bb28a 100644 --- a/tests/ui/print_literal.rs +++ b/tests/ui/print_literal.rs @@ -17,6 +17,9 @@ fn main() { println!("{bar:8} {foo:>8}", foo = "hello", bar = "world"); println!("{number:>width$}", number = 1, width = 6); println!("{number:>0width$}", number = 1, width = 6); + println!("{} of {:b} people know binary, the other half doesn't", 1, 2); + println!("10 / 4 is {}", 2.5); + println!("2 + 1 = {}", 3); // these should throw warnings print!("Hello {}", "world"); diff --git a/tests/ui/print_literal.stderr b/tests/ui/print_literal.stderr index 692abdb3054..e284aece236 100644 --- a/tests/ui/print_literal.stderr +++ b/tests/ui/print_literal.stderr @@ -1,5 +1,5 @@ error: literal with an empty format string - --> $DIR/print_literal.rs:22:24 + --> $DIR/print_literal.rs:25:24 | LL | print!("Hello {}", "world"); | ^^^^^^^ @@ -7,61 +7,61 @@ LL | print!("Hello {}", "world"); = note: `-D clippy::print-literal` implied by `-D warnings` error: literal with an empty format string - --> $DIR/print_literal.rs:23:36 + --> $DIR/print_literal.rs:26:36 | LL | println!("Hello {} {}", world, "world"); | ^^^^^^^ error: literal with an empty format string - --> $DIR/print_literal.rs:24:26 + --> $DIR/print_literal.rs:27:26 | LL | println!("Hello {}", "world"); | ^^^^^^^ error: literal with an empty format string - --> $DIR/print_literal.rs:29:25 + --> $DIR/print_literal.rs:32:25 | LL | println!("{0} {1}", "hello", "world"); | ^^^^^^^ error: literal with an empty format string - --> $DIR/print_literal.rs:29:34 + --> $DIR/print_literal.rs:32:34 | LL | println!("{0} {1}", "hello", "world"); | ^^^^^^^ error: literal with an empty format string - --> $DIR/print_literal.rs:30:25 + --> $DIR/print_literal.rs:33:25 | LL | println!("{1} {0}", "hello", "world"); | ^^^^^^^ error: literal with an empty format string - --> $DIR/print_literal.rs:30:34 + --> $DIR/print_literal.rs:33:34 | LL | println!("{1} {0}", "hello", "world"); | ^^^^^^^ error: literal with an empty format string - --> $DIR/print_literal.rs:33:35 + --> $DIR/print_literal.rs:36:35 | LL | println!("{foo} {bar}", foo = "hello", bar = "world"); | ^^^^^^^ error: literal with an empty format string - --> $DIR/print_literal.rs:33:50 + --> $DIR/print_literal.rs:36:50 | LL | println!("{foo} {bar}", foo = "hello", bar = "world"); | ^^^^^^^ error: literal with an empty format string - --> $DIR/print_literal.rs:34:35 + --> $DIR/print_literal.rs:37:35 | LL | println!("{bar} {foo}", foo = "hello", bar = "world"); | ^^^^^^^ error: literal with an empty format string - --> $DIR/print_literal.rs:34:50 + --> $DIR/print_literal.rs:37:50 | LL | println!("{bar} {foo}", foo = "hello", bar = "world"); | ^^^^^^^ diff --git a/tests/ui/write_literal.rs b/tests/ui/write_literal.rs index f5de7ea71d3..0a127858def 100644 --- a/tests/ui/write_literal.rs +++ b/tests/ui/write_literal.rs @@ -22,6 +22,9 @@ fn main() { writeln!(&mut v, "{bar:8} {foo:>8}", foo = "hello", bar = "world"); writeln!(&mut v, "{number:>width$}", number = 1, width = 6); writeln!(&mut v, "{number:>0width$}", number = 1, width = 6); + writeln!(&mut v, "{} of {:b} people know binary, the other half doesn't", 1, 2); + writeln!(&mut v, "10 / 4 is {}", 2.5); + writeln!(&mut v, "2 + 1 = {}", 3); // these should throw warnings write!(&mut v, "Hello {}", "world"); diff --git a/tests/ui/write_literal.stderr b/tests/ui/write_literal.stderr index 4dcaaa474a8..e54d89ecf29 100644 --- a/tests/ui/write_literal.stderr +++ b/tests/ui/write_literal.stderr @@ -1,5 +1,5 @@ error: literal with an empty format string - --> $DIR/write_literal.rs:27:32 + --> $DIR/write_literal.rs:30:32 | LL | write!(&mut v, "Hello {}", "world"); | ^^^^^^^ @@ -7,61 +7,61 @@ LL | write!(&mut v, "Hello {}", "world"); = note: `-D clippy::write-literal` implied by `-D warnings` error: literal with an empty format string - --> $DIR/write_literal.rs:28:44 + --> $DIR/write_literal.rs:31:44 | LL | writeln!(&mut v, "Hello {} {}", world, "world"); | ^^^^^^^ error: literal with an empty format string - --> $DIR/write_literal.rs:29:34 + --> $DIR/write_literal.rs:32:34 | LL | writeln!(&mut v, "Hello {}", "world"); | ^^^^^^^ error: literal with an empty format string - --> $DIR/write_literal.rs:34:33 + --> $DIR/write_literal.rs:37:33 | LL | writeln!(&mut v, "{0} {1}", "hello", "world"); | ^^^^^^^ error: literal with an empty format string - --> $DIR/write_literal.rs:34:42 + --> $DIR/write_literal.rs:37:42 | LL | writeln!(&mut v, "{0} {1}", "hello", "world"); | ^^^^^^^ error: literal with an empty format string - --> $DIR/write_literal.rs:35:33 + --> $DIR/write_literal.rs:38:33 | LL | writeln!(&mut v, "{1} {0}", "hello", "world"); | ^^^^^^^ error: literal with an empty format string - --> $DIR/write_literal.rs:35:42 + --> $DIR/write_literal.rs:38:42 | LL | writeln!(&mut v, "{1} {0}", "hello", "world"); | ^^^^^^^ error: literal with an empty format string - --> $DIR/write_literal.rs:38:43 + --> $DIR/write_literal.rs:41:43 | LL | writeln!(&mut v, "{foo} {bar}", foo = "hello", bar = "world"); | ^^^^^^^ error: literal with an empty format string - --> $DIR/write_literal.rs:38:58 + --> $DIR/write_literal.rs:41:58 | LL | writeln!(&mut v, "{foo} {bar}", foo = "hello", bar = "world"); | ^^^^^^^ error: literal with an empty format string - --> $DIR/write_literal.rs:39:43 + --> $DIR/write_literal.rs:42:43 | LL | writeln!(&mut v, "{bar} {foo}", foo = "hello", bar = "world"); | ^^^^^^^ error: literal with an empty format string - --> $DIR/write_literal.rs:39:58 + --> $DIR/write_literal.rs:42:58 | LL | writeln!(&mut v, "{bar} {foo}", foo = "hello", bar = "world"); | ^^^^^^^