Auto merge of #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 #6335
This commit is contained in:
bors 2021-01-21 14:23:25 +00:00
commit 7b50a4ead7
5 changed files with 26 additions and 58 deletions

View file

@ -2,7 +2,8 @@ use std::borrow::Cow;
use std::ops::Range; use std::ops::Range;
use crate::utils::{snippet_with_applicability, span_lint, span_lint_and_sugg, span_lint_and_then}; 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::token;
use rustc_ast::tokenstream::TokenStream; use rustc_ast::tokenstream::TokenStream;
use rustc_errors::Applicability; use rustc_errors::Applicability;
@ -437,7 +438,7 @@ impl Write {
return (Some(fmtstr), None); return (Some(fmtstr), None);
}; };
match &token_expr.kind { match &token_expr.kind {
ExprKind::Lit(_) => { ExprKind::Lit(lit) if !matches!(lit.kind, LitKind::Int(..) | LitKind::Float(..)) => {
let mut all_simple = true; let mut all_simple = true;
let mut seen = false; let mut seen = false;
for arg in &args { for arg in &args {
@ -457,8 +458,11 @@ impl Write {
idx += 1; idx += 1;
}, },
ExprKind::Assign(lhs, rhs, _) => { ExprKind::Assign(lhs, rhs, _) => {
if let ExprKind::Lit(_) = rhs.kind { if_chain! {
if let ExprKind::Path(_, p) = &lhs.kind { 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 all_simple = true;
let mut seen = false; let mut seen = false;
for arg in &args { for arg in &args {

View file

@ -17,14 +17,14 @@ fn main() {
println!("{bar:8} {foo:>8}", foo = "hello", bar = "world"); println!("{bar:8} {foo:>8}", foo = "hello", bar = "world");
println!("{number:>width$}", number = 1, width = 6); println!("{number:>width$}", number = 1, width = 6);
println!("{number:>0width$}", 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 // these should throw warnings
println!("{} of {:b} people know binary, the other half doesn't", 1, 2);
print!("Hello {}", "world"); print!("Hello {}", "world");
println!("Hello {} {}", world, "world"); println!("Hello {} {}", world, "world");
println!("Hello {}", "world"); println!("Hello {}", "world");
println!("10 / 4 is {}", 2.5);
println!("2 + 1 = {}", 3);
// positional args don't change the fact // positional args don't change the fact
// that we're using a literal -- this should // that we're using a literal -- this should

View file

@ -1,41 +1,23 @@
error: literal with an empty format string 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` = note: `-D clippy::print-literal` implied by `-D warnings`
error: literal with an empty format string error: literal with an empty format string
--> $DIR/print_literal.rs:23:24 --> $DIR/print_literal.rs:26:36
|
LL | print!("Hello {}", "world");
| ^^^^^^^
error: literal with an empty format string
--> $DIR/print_literal.rs:24:36
| |
LL | println!("Hello {} {}", world, "world"); LL | println!("Hello {} {}", world, "world");
| ^^^^^^^ | ^^^^^^^
error: literal with an empty format string error: literal with an empty format string
--> $DIR/print_literal.rs:25:26 --> $DIR/print_literal.rs:27:26
| |
LL | println!("Hello {}", "world"); 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 error: literal with an empty format string
--> $DIR/print_literal.rs:32:25 --> $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"); LL | println!("{bar} {foo}", foo = "hello", bar = "world");
| ^^^^^^^ | ^^^^^^^
error: aborting due to 14 previous errors error: aborting due to 11 previous errors

View file

@ -22,14 +22,14 @@ fn main() {
writeln!(&mut v, "{bar:8} {foo:>8}", foo = "hello", bar = "world"); writeln!(&mut v, "{bar:8} {foo:>8}", foo = "hello", bar = "world");
writeln!(&mut v, "{number:>width$}", number = 1, width = 6); writeln!(&mut v, "{number:>width$}", number = 1, width = 6);
writeln!(&mut v, "{number:>0width$}", 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 // these should throw warnings
writeln!(&mut v, "{} of {:b} people know binary, the other half doesn't", 1, 2);
write!(&mut v, "Hello {}", "world"); write!(&mut v, "Hello {}", "world");
writeln!(&mut v, "Hello {} {}", world, "world"); writeln!(&mut v, "Hello {} {}", world, "world");
writeln!(&mut v, "Hello {}", "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 // positional args don't change the fact
// that we're using a literal -- this should // that we're using a literal -- this should

View file

@ -1,41 +1,23 @@
error: literal with an empty format string 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` = note: `-D clippy::write-literal` implied by `-D warnings`
error: literal with an empty format string error: literal with an empty format string
--> $DIR/write_literal.rs:28:32 --> $DIR/write_literal.rs:31:44
|
LL | write!(&mut v, "Hello {}", "world");
| ^^^^^^^
error: literal with an empty format string
--> $DIR/write_literal.rs:29:44
| |
LL | writeln!(&mut v, "Hello {} {}", world, "world"); LL | writeln!(&mut v, "Hello {} {}", world, "world");
| ^^^^^^^ | ^^^^^^^
error: literal with an empty format string 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"); 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 error: literal with an empty format string
--> $DIR/write_literal.rs:37:33 --> $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"); LL | writeln!(&mut v, "{bar} {foo}", foo = "hello", bar = "world");
| ^^^^^^^ | ^^^^^^^
error: aborting due to 14 previous errors error: aborting due to 11 previous errors