diff --git a/clippy_lints/src/write.rs b/clippy_lints/src/write.rs index af324f831..6358104ee 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; @@ -437,7 +438,7 @@ impl Write { return (Some(fmtstr), None); }; match &token_expr.kind { - ExprKind::Lit(_) => { + ExprKind::Lit(lit) if !matches!(lit.kind, LitKind::Int(..) | LitKind::Float(..)) => { let mut all_simple = true; let mut seen = false; for arg in &args { @@ -457,8 +458,11 @@ impl Write { 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 !matches!(lit.kind, LitKind::Int(..) | LitKind::Float(..)); + if let ExprKind::Path(_, p) = &lhs.kind; + then { let mut all_simple = true; let mut seen = false; for arg in &args { diff --git a/tests/ui/print_literal.rs b/tests/ui/print_literal.rs index 40ed18e93..8665a3bb2 100644 --- a/tests/ui/print_literal.rs +++ b/tests/ui/print_literal.rs @@ -17,14 +17,14 @@ 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 - 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 fc502e9f7..e284aece2 100644 --- a/tests/ui/print_literal.stderr +++ b/tests/ui/print_literal.stderr @@ -1,41 +1,23 @@ error: literal with an empty format string - --> $DIR/print_literal.rs:22:71 + --> $DIR/print_literal.rs:25: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:26:36 | LL | println!("Hello {} {}", world, "world"); | ^^^^^^^ error: literal with an empty format string - --> $DIR/print_literal.rs:25:26 + --> $DIR/print_literal.rs:27: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 | @@ -84,5 +66,5 @@ error: literal with an empty format string 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 d8205c5eb..0a127858d 100644 --- a/tests/ui/write_literal.rs +++ b/tests/ui/write_literal.rs @@ -22,14 +22,14 @@ 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 - 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 54a787fe5..e54d89ecf 100644 --- a/tests/ui/write_literal.stderr +++ b/tests/ui/write_literal.stderr @@ -1,41 +1,23 @@ error: literal with an empty format string - --> $DIR/write_literal.rs:27:79 + --> $DIR/write_literal.rs:30: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:31: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:32: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 | @@ -84,5 +66,5 @@ error: literal with an empty format string LL | writeln!(&mut v, "{bar} {foo}", foo = "hello", bar = "world"); | ^^^^^^^ -error: aborting due to 14 previous errors +error: aborting due to 11 previous errors