Skip to content

Commit 7b50a4e

Browse files
committed
Auto merge of rust-lang#6408 - pro-grammer1:master, r=oli-obk
Fix false positive in write_literal and print_literal (numeric literals) changelog: No longer lint numeric literals in [`write_literal`] and [`print_literal`]. Fixes rust-lang#6335
2 parents 16d13a5 + 32b2a3f commit 7b50a4e

File tree

5 files changed

+26
-58
lines changed

5 files changed

+26
-58
lines changed

clippy_lints/src/write.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use std::borrow::Cow;
22
use std::ops::Range;
33

44
use crate::utils::{snippet_with_applicability, span_lint, span_lint_and_sugg, span_lint_and_then};
5-
use rustc_ast::ast::{Expr, ExprKind, Item, ItemKind, MacCall, StrLit, StrStyle};
5+
use if_chain::if_chain;
6+
use rustc_ast::ast::{Expr, ExprKind, Item, ItemKind, LitKind, MacCall, StrLit, StrStyle};
67
use rustc_ast::token;
78
use rustc_ast::tokenstream::TokenStream;
89
use rustc_errors::Applicability;
@@ -437,7 +438,7 @@ impl Write {
437438
return (Some(fmtstr), None);
438439
};
439440
match &token_expr.kind {
440-
ExprKind::Lit(_) => {
441+
ExprKind::Lit(lit) if !matches!(lit.kind, LitKind::Int(..) | LitKind::Float(..)) => {
441442
let mut all_simple = true;
442443
let mut seen = false;
443444
for arg in &args {
@@ -457,8 +458,11 @@ impl Write {
457458
idx += 1;
458459
},
459460
ExprKind::Assign(lhs, rhs, _) => {
460-
if let ExprKind::Lit(_) = rhs.kind {
461-
if let ExprKind::Path(_, p) = &lhs.kind {
461+
if_chain! {
462+
if let ExprKind::Lit(ref lit) = rhs.kind;
463+
if !matches!(lit.kind, LitKind::Int(..) | LitKind::Float(..));
464+
if let ExprKind::Path(_, p) = &lhs.kind;
465+
then {
462466
let mut all_simple = true;
463467
let mut seen = false;
464468
for arg in &args {

tests/ui/print_literal.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ fn main() {
1717
println!("{bar:8} {foo:>8}", foo = "hello", bar = "world");
1818
println!("{number:>width$}", number = 1, width = 6);
1919
println!("{number:>0width$}", number = 1, width = 6);
20+
println!("{} of {:b} people know binary, the other half doesn't", 1, 2);
21+
println!("10 / 4 is {}", 2.5);
22+
println!("2 + 1 = {}", 3);
2023

2124
// these should throw warnings
22-
println!("{} of {:b} people know binary, the other half doesn't", 1, 2);
2325
print!("Hello {}", "world");
2426
println!("Hello {} {}", world, "world");
2527
println!("Hello {}", "world");
26-
println!("10 / 4 is {}", 2.5);
27-
println!("2 + 1 = {}", 3);
2828

2929
// positional args don't change the fact
3030
// that we're using a literal -- this should

tests/ui/print_literal.stderr

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,23 @@
11
error: literal with an empty format string
2-
--> $DIR/print_literal.rs:22:71
3-
|
4-
LL | println!("{} of {:b} people know binary, the other half doesn't", 1, 2);
5-
| ^
6-
|
7-
= note: `-D clippy::print-literal` implied by `-D warnings`
8-
9-
error: literal with an empty format string
10-
--> $DIR/print_literal.rs:23:24
2+
--> $DIR/print_literal.rs:25:24
113
|
124
LL | print!("Hello {}", "world");
135
| ^^^^^^^
6+
|
7+
= note: `-D clippy::print-literal` implied by `-D warnings`
148

159
error: literal with an empty format string
16-
--> $DIR/print_literal.rs:24:36
10+
--> $DIR/print_literal.rs:26:36
1711
|
1812
LL | println!("Hello {} {}", world, "world");
1913
| ^^^^^^^
2014

2115
error: literal with an empty format string
22-
--> $DIR/print_literal.rs:25:26
16+
--> $DIR/print_literal.rs:27:26
2317
|
2418
LL | println!("Hello {}", "world");
2519
| ^^^^^^^
2620

27-
error: literal with an empty format string
28-
--> $DIR/print_literal.rs:26:30
29-
|
30-
LL | println!("10 / 4 is {}", 2.5);
31-
| ^^^
32-
33-
error: literal with an empty format string
34-
--> $DIR/print_literal.rs:27:28
35-
|
36-
LL | println!("2 + 1 = {}", 3);
37-
| ^
38-
3921
error: literal with an empty format string
4022
--> $DIR/print_literal.rs:32:25
4123
|
@@ -84,5 +66,5 @@ error: literal with an empty format string
8466
LL | println!("{bar} {foo}", foo = "hello", bar = "world");
8567
| ^^^^^^^
8668

87-
error: aborting due to 14 previous errors
69+
error: aborting due to 11 previous errors
8870

tests/ui/write_literal.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ fn main() {
2222
writeln!(&mut v, "{bar:8} {foo:>8}", foo = "hello", bar = "world");
2323
writeln!(&mut v, "{number:>width$}", number = 1, width = 6);
2424
writeln!(&mut v, "{number:>0width$}", number = 1, width = 6);
25+
writeln!(&mut v, "{} of {:b} people know binary, the other half doesn't", 1, 2);
26+
writeln!(&mut v, "10 / 4 is {}", 2.5);
27+
writeln!(&mut v, "2 + 1 = {}", 3);
2528

2629
// these should throw warnings
27-
writeln!(&mut v, "{} of {:b} people know binary, the other half doesn't", 1, 2);
2830
write!(&mut v, "Hello {}", "world");
2931
writeln!(&mut v, "Hello {} {}", world, "world");
3032
writeln!(&mut v, "Hello {}", "world");
31-
writeln!(&mut v, "10 / 4 is {}", 2.5);
32-
writeln!(&mut v, "2 + 1 = {}", 3);
3333

3434
// positional args don't change the fact
3535
// that we're using a literal -- this should

tests/ui/write_literal.stderr

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,23 @@
11
error: literal with an empty format string
2-
--> $DIR/write_literal.rs:27:79
3-
|
4-
LL | writeln!(&mut v, "{} of {:b} people know binary, the other half doesn't", 1, 2);
5-
| ^
6-
|
7-
= note: `-D clippy::write-literal` implied by `-D warnings`
8-
9-
error: literal with an empty format string
10-
--> $DIR/write_literal.rs:28:32
2+
--> $DIR/write_literal.rs:30:32
113
|
124
LL | write!(&mut v, "Hello {}", "world");
135
| ^^^^^^^
6+
|
7+
= note: `-D clippy::write-literal` implied by `-D warnings`
148

159
error: literal with an empty format string
16-
--> $DIR/write_literal.rs:29:44
10+
--> $DIR/write_literal.rs:31:44
1711
|
1812
LL | writeln!(&mut v, "Hello {} {}", world, "world");
1913
| ^^^^^^^
2014

2115
error: literal with an empty format string
22-
--> $DIR/write_literal.rs:30:34
16+
--> $DIR/write_literal.rs:32:34
2317
|
2418
LL | writeln!(&mut v, "Hello {}", "world");
2519
| ^^^^^^^
2620

27-
error: literal with an empty format string
28-
--> $DIR/write_literal.rs:31:38
29-
|
30-
LL | writeln!(&mut v, "10 / 4 is {}", 2.5);
31-
| ^^^
32-
33-
error: literal with an empty format string
34-
--> $DIR/write_literal.rs:32:36
35-
|
36-
LL | writeln!(&mut v, "2 + 1 = {}", 3);
37-
| ^
38-
3921
error: literal with an empty format string
4022
--> $DIR/write_literal.rs:37:33
4123
|
@@ -84,5 +66,5 @@ error: literal with an empty format string
8466
LL | writeln!(&mut v, "{bar} {foo}", foo = "hello", bar = "world");
8567
| ^^^^^^^
8668

87-
error: aborting due to 14 previous errors
69+
error: aborting due to 11 previous errors
8870

0 commit comments

Comments
 (0)