Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix false positive in write_literal and print_literal (numeric literals) #6408

Merged
merged 6 commits into from
Jan 21, 2021
22 changes: 17 additions & 5 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 @@ -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,
} =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ExprKind::Lit(lit)
if match lit.kind {
LitKind::Int(_, _) | LitKind::Float(_, _) => false,
_ => true,
} =>
ExprKind::Lit(lit) if !matches!(lit.kind, LitKind::Int(..) | LitKind::Float(..)) => {

We can simplify using matches!()

{
let mut all_simple = true;
let mut seen = false;
for arg in &args {
Expand All @@ -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,
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if match lit.kind {
LitKind::Int(_, _) | LitKind::Float(_, _) => false,
_ => true,
};
if !matches!(lit.kind, LitKind::Int(..) | LitKind::Float(..));

Same here

if let ExprKind::Path(_, p) = &lhs.kind;
then {
let mut all_simple = true;
let mut seen = false;
for arg in &args {
Expand Down