This commit is contained in:
flip1995
2019-11-07 13:27:00 +01:00
parent 305ba73fc1
commit e917b01286
9 changed files with 30 additions and 22 deletions

View File

@@ -3,7 +3,7 @@ use rustc::hir::*;
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
use rustc::{declare_lint_pass, declare_tool_lint}; use rustc::{declare_lint_pass, declare_tool_lint};
use std::f64::consts as f64; use std::f64::consts as f64;
use syntax::ast::{FloatTy, LitKind}; use syntax::ast::{FloatTy, LitFloatType, LitKind};
use syntax::symbol; use syntax::symbol;
declare_clippy_lint! { declare_clippy_lint! {
@@ -62,9 +62,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ApproxConstant {
fn check_lit(cx: &LateContext<'_, '_>, lit: &LitKind, e: &Expr) { fn check_lit(cx: &LateContext<'_, '_>, lit: &LitKind, e: &Expr) {
match *lit { match *lit {
LitKind::Float(s, FloatTy::F32) => check_known_consts(cx, e, s, "f32"), LitKind::Float(s, LitFloatType::Suffixed(fty)) => match fty {
LitKind::Float(s, FloatTy::F64) => check_known_consts(cx, e, s, "f64"), FloatTy::F32 => check_known_consts(cx, e, s, "f32"),
LitKind::FloatUnsuffixed(s) => check_known_consts(cx, e, s, "f{32, 64}"), FloatTy::F64 => check_known_consts(cx, e, s, "f64"),
},
LitKind::Float(s, LitFloatType::Unsuffixed) => check_known_consts(cx, e, s, "f{32, 64}"),
_ => (), _ => (),
} }
} }

View File

@@ -161,9 +161,11 @@ pub fn lit_to_constant(lit: &LitKind, ty: Option<Ty<'_>>) -> Constant {
LitKind::ByteStr(ref s) => Constant::Binary(Lrc::clone(s)), LitKind::ByteStr(ref s) => Constant::Binary(Lrc::clone(s)),
LitKind::Char(c) => Constant::Char(c), LitKind::Char(c) => Constant::Char(c),
LitKind::Int(n, _) => Constant::Int(n), LitKind::Int(n, _) => Constant::Int(n),
LitKind::Float(ref is, FloatTy::F32) => Constant::F32(is.as_str().parse().unwrap()), LitKind::Float(ref is, LitFloatType::Suffixed(fty)) => match fty {
LitKind::Float(ref is, FloatTy::F64) => Constant::F64(is.as_str().parse().unwrap()), FloatTy::F32 => Constant::F32(is.as_str().parse().unwrap()),
LitKind::FloatUnsuffixed(ref is) => match ty.expect("type of float is known").kind { FloatTy::F64 => Constant::F64(is.as_str().parse().unwrap()),
},
LitKind::Float(ref is, LitFloatType::Unsuffixed) => match ty.expect("type of float is known").kind {
ty::Float(FloatTy::F32) => Constant::F32(is.as_str().parse().unwrap()), ty::Float(FloatTy::F32) => Constant::F32(is.as_str().parse().unwrap()),
ty::Float(FloatTy::F64) => Constant::F64(is.as_str().parse().unwrap()), ty::Float(FloatTy::F64) => Constant::F64(is.as_str().parse().unwrap()),
_ => bug!(), _ => bug!(),

View File

@@ -43,7 +43,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ExcessivePrecision {
let ty = cx.tables.expr_ty(expr); let ty = cx.tables.expr_ty(expr);
if let ty::Float(fty) = ty.kind; if let ty::Float(fty) = ty.kind;
if let hir::ExprKind::Lit(ref lit) = expr.kind; if let hir::ExprKind::Lit(ref lit) = expr.kind;
if let LitKind::Float(sym, _) | LitKind::FloatUnsuffixed(sym) = lit.node; if let LitKind::Float(sym, _) = lit.node;
if let Some(sugg) = Self::check(sym, fty); if let Some(sugg) = Self::check(sym, fty);
then { then {
span_lint_and_sugg( span_lint_and_sugg(

View File

@@ -373,7 +373,7 @@ impl LiteralDigitGrouping {
} }
} }
}, },
LitKind::Float(..) | LitKind::FloatUnsuffixed(..) => { LitKind::Float(..) => {
// Lint floating-point literals. // Lint floating-point literals.
if_chain! { if_chain! {
if let Some(src) = snippet_opt(cx, lit.span); if let Some(src) = snippet_opt(cx, lit.span);

View File

@@ -482,8 +482,8 @@ impl MiscEarlyLints {
if let LitKind::Int(value, lit_int_type) = lit.kind { if let LitKind::Int(value, lit_int_type) = lit.kind {
let suffix = match lit_int_type { let suffix = match lit_int_type {
LitIntType::Signed(ty) => ty.ty_to_string(), LitIntType::Signed(ty) => ty.name_str(),
LitIntType::Unsigned(ty) => ty.ty_to_string(), LitIntType::Unsigned(ty) => ty.name_str(),
LitIntType::Unsuffixed => "", LitIntType::Unsuffixed => "",
}; };
@@ -543,8 +543,8 @@ impl MiscEarlyLints {
}, },
); );
} }
} else if let LitKind::Float(_, float_ty) = lit.kind { } else if let LitKind::Float(_, LitFloatType::Suffixed(float_ty)) = lit.kind {
let suffix = float_ty.ty_to_string(); let suffix = float_ty.name_str();
let maybe_last_sep_idx = lit_snip.len() - suffix.len() - 1; let maybe_last_sep_idx = lit_snip.len() - suffix.len() - 1;
if lit_snip.as_bytes()[maybe_last_sep_idx] != b'_' { if lit_snip.as_bytes()[maybe_last_sep_idx] != b'_' {
span_lint_and_sugg( span_lint_and_sugg(

View File

@@ -90,7 +90,7 @@ impl EarlyLintPass for Precedence {
if let Some(slf) = args.first() { if let Some(slf) = args.first() {
if let ExprKind::Lit(ref lit) = slf.kind { if let ExprKind::Lit(ref lit) = slf.kind {
match lit.kind { match lit.kind {
LitKind::Int(..) | LitKind::Float(..) | LitKind::FloatUnsuffixed(..) => { LitKind::Int(..) | LitKind::Float(..) => {
let mut applicability = Applicability::MachineApplicable; let mut applicability = Applicability::MachineApplicable;
span_lint_and_sugg( span_lint_and_sugg(
cx, cx,

View File

@@ -390,7 +390,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Transmute {
|db| { |db| {
let arg = sugg::Sugg::hir(cx, &args[0], ".."); let arg = sugg::Sugg::hir(cx, &args[0], "..");
let arg = if let ty::Int(_) = from_ty.kind { let arg = if let ty::Int(_) = from_ty.kind {
arg.as_ty(ast::UintTy::U32) arg.as_ty(ast::UintTy::U32.name_str())
} else { } else {
arg arg
}; };

View File

@@ -15,7 +15,7 @@ use rustc::{declare_lint_pass, declare_tool_lint, impl_lint_pass};
use rustc_errors::Applicability; use rustc_errors::Applicability;
use rustc_target::spec::abi::Abi; use rustc_target::spec::abi::Abi;
use rustc_typeck::hir_ty_to_ty; use rustc_typeck::hir_ty_to_ty;
use syntax::ast::{FloatTy, IntTy, LitIntType, LitKind, UintTy}; use syntax::ast::{FloatTy, IntTy, LitFloatType, LitIntType, LitKind, UintTy};
use syntax::errors::DiagnosticBuilder; use syntax::errors::DiagnosticBuilder;
use syntax::source_map::Span; use syntax::source_map::Span;
use syntax::symbol::{sym, Symbol}; use syntax::symbol::{sym, Symbol};
@@ -1186,7 +1186,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Casts {
} }
} }
match lit.node { match lit.node {
LitKind::Int(_, LitIntType::Unsuffixed) | LitKind::FloatUnsuffixed(_) => {}, LitKind::Int(_, LitIntType::Unsuffixed) | LitKind::Float(_, LitFloatType::Unsuffixed) => {},
_ => { _ => {
if cast_from.kind == cast_to.kind && !in_external_macro(cx.sess(), expr.span) { if cast_from.kind == cast_to.kind && !in_external_macro(cx.sess(), expr.span) {
span_lint( span_lint(

View File

@@ -9,7 +9,7 @@ use rustc::lint::{LateContext, LateLintPass, LintArray, LintContext, LintPass};
use rustc::session::Session; use rustc::session::Session;
use rustc::{declare_lint_pass, declare_tool_lint}; use rustc::{declare_lint_pass, declare_tool_lint};
use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::fx::FxHashMap;
use syntax::ast::{Attribute, LitKind}; use syntax::ast::{Attribute, LitFloatType, LitKind};
declare_clippy_lint! { declare_clippy_lint! {
/// **What it does:** Generates clippy code that detects the offending pattern /// **What it does:** Generates clippy code that detects the offending pattern
@@ -288,10 +288,14 @@ impl<'tcx> Visitor<'tcx> for PrintVisitor {
LitKind::Byte(b) => println!(" if let LitKind::Byte({}) = {}.node;", b, lit_pat), LitKind::Byte(b) => println!(" if let LitKind::Byte({}) = {}.node;", b, lit_pat),
// FIXME: also check int type // FIXME: also check int type
LitKind::Int(i, _) => println!(" if let LitKind::Int({}, _) = {}.node;", i, lit_pat), LitKind::Int(i, _) => println!(" if let LitKind::Int({}, _) = {}.node;", i, lit_pat),
LitKind::Float(..) => println!(" if let LitKind::Float(..) = {}.node;", lit_pat), LitKind::Float(_, LitFloatType::Suffixed(_)) => println!(
LitKind::FloatUnsuffixed(_) => { " if let LitKind::Float(_, LitFloatType::Suffixed(_)) = {}.node;",
println!(" if let LitKind::FloatUnsuffixed(_) = {}.node;", lit_pat) lit_pat
}, ),
LitKind::Float(_, LitFloatType::Unsuffixed) => println!(
" if let LitKind::Float(_, LitFloatType::Unsuffixed) = {}.node;",
lit_pat
),
LitKind::ByteStr(ref vec) => { LitKind::ByteStr(ref vec) => {
let vec_pat = self.next("vec"); let vec_pat = self.next("vec");
println!(" if let LitKind::ByteStr(ref {}) = {}.node;", vec_pat, lit_pat); println!(" if let LitKind::ByteStr(ref {}) = {}.node;", vec_pat, lit_pat);