Skip to content

Commit

Permalink
Auto merge of #6408 - pro-grammer1:master, r=oli-obk
Browse files Browse the repository at this point in the history
Fix false positive in write_literal and print_literal (numeric literals)

changelog: No longer lint numeric literals in [`write_literal`] and [`print_literal`].

Fixes #6335
  • Loading branch information
bors committed Jan 21, 2021
2 parents 16d13a5 + 32b2a3f commit 7b50a4e
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 58 deletions.
12 changes: 8 additions & 4 deletions clippy_lints/src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/print_literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 6 additions & 24 deletions tests/ui/print_literal.stderr
Original file line number Diff line number Diff line change
@@ -1,41 +1,23 @@
error: literal with an empty format string
--> $DIR/print_literal.rs:22:71
|
LL | println!("{} of {:b} people know binary, the other half doesn't", 1, 2);
| ^
|
= note: `-D clippy::print-literal` implied by `-D warnings`

error: literal with an empty format string
--> $DIR/print_literal.rs:23:24
--> $DIR/print_literal.rs:25:24
|
LL | print!("Hello {}", "world");
| ^^^^^^^
|
= note: `-D clippy::print-literal` implied by `-D warnings`

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
|
Expand Down Expand Up @@ -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

6 changes: 3 additions & 3 deletions tests/ui/write_literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 6 additions & 24 deletions tests/ui/write_literal.stderr
Original file line number Diff line number Diff line change
@@ -1,41 +1,23 @@
error: literal with an empty format string
--> $DIR/write_literal.rs:27:79
|
LL | writeln!(&mut v, "{} of {:b} people know binary, the other half doesn't", 1, 2);
| ^
|
= note: `-D clippy::write-literal` implied by `-D warnings`

error: literal with an empty format string
--> $DIR/write_literal.rs:28:32
--> $DIR/write_literal.rs:30:32
|
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: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
|
Expand Down Expand Up @@ -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

0 comments on commit 7b50a4e

Please sign in to comment.