This commit is contained in:
Oliver Scherer
2019-05-18 01:34:52 +02:00
parent 462df72100
commit f49ef0ec04
6 changed files with 73 additions and 75 deletions

View File

@@ -72,7 +72,7 @@ fn check_lit(cx: &LateContext<'_, '_>, lit: &LitKind, e: &Expr) {
fn check_known_consts(cx: &LateContext<'_, '_>, e: &Expr, s: symbol::Symbol, module: &str) { fn check_known_consts(cx: &LateContext<'_, '_>, e: &Expr, s: symbol::Symbol, module: &str) {
let s = s.as_str(); let s = s.as_str();
if s.parse::<f64>().is_ok() { if s.parse::<f64>().is_ok() {
for &(constant, name, min_digits) in KNOWN_CONSTS.iter() { for &(constant, name, min_digits) in &KNOWN_CONSTS {
if is_approx_const(constant, &s, min_digits) { if is_approx_const(constant, &s, min_digits) {
span_lint( span_lint(
cx, cx,

View File

@@ -139,7 +139,7 @@ const HEURISTICS: [(&str, usize, Heuristic, Finiteness); 19] = [
fn is_infinite(cx: &LateContext<'_, '_>, expr: &Expr) -> Finiteness { fn is_infinite(cx: &LateContext<'_, '_>, expr: &Expr) -> Finiteness {
match expr.node { match expr.node {
ExprKind::MethodCall(ref method, _, ref args) => { ExprKind::MethodCall(ref method, _, ref args) => {
for &(name, len, heuristic, cap) in HEURISTICS.iter() { for &(name, len, heuristic, cap) in &HEURISTICS {
if method.ident.name.as_str() == name && args.len() == len { if method.ident.name.as_str() == name && args.len() == len {
return (match heuristic { return (match heuristic {
Always => Infinite, Always => Infinite,
@@ -215,12 +215,12 @@ const INFINITE_COLLECTORS: [&[&str]; 8] = [
fn complete_infinite_iter(cx: &LateContext<'_, '_>, expr: &Expr) -> Finiteness { fn complete_infinite_iter(cx: &LateContext<'_, '_>, expr: &Expr) -> Finiteness {
match expr.node { match expr.node {
ExprKind::MethodCall(ref method, _, ref args) => { ExprKind::MethodCall(ref method, _, ref args) => {
for &(name, len) in COMPLETING_METHODS.iter() { for &(name, len) in &COMPLETING_METHODS {
if method.ident.name.as_str() == name && args.len() == len { if method.ident.name.as_str() == name && args.len() == len {
return is_infinite(cx, &args[0]); return is_infinite(cx, &args[0]);
} }
} }
for &(name, len) in POSSIBLY_COMPLETING_METHODS.iter() { for &(name, len) in &POSSIBLY_COMPLETING_METHODS {
if method.ident.name.as_str() == name && args.len() == len { if method.ident.name.as_str() == name && args.len() == len {
return MaybeInfinite.and(is_infinite(cx, &args[0])); return MaybeInfinite.and(is_infinite(cx, &args[0]));
} }

View File

@@ -6,7 +6,6 @@ use std::fmt;
use std::iter; use std::iter;
use if_chain::if_chain; use if_chain::if_chain;
use lazy_static::lazy_static;
use matches::matches; use matches::matches;
use rustc::hir; use rustc::hir;
use rustc::hir::def::{DefKind, Res}; use rustc::hir::def::{DefKind, Res};
@@ -17,7 +16,7 @@ use rustc::{declare_lint_pass, declare_tool_lint};
use rustc_errors::Applicability; use rustc_errors::Applicability;
use syntax::ast; use syntax::ast;
use syntax::source_map::{BytePos, Span}; use syntax::source_map::{BytePos, Span};
use syntax::symbol::{LocalInternedString, Symbol}; use syntax::symbol::LocalInternedString;
use crate::utils::paths; use crate::utils::paths;
use crate::utils::sugg; use crate::utils::sugg;
@@ -914,8 +913,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
match self_ty.sty { match self_ty.sty {
ty::Ref(_, ty, _) if ty.sty == ty::Str => { ty::Ref(_, ty, _) if ty.sty == ty::Str => {
for &(method, pos) in PATTERN_METHODS.iter() { for &(method, pos) in &PATTERN_METHODS {
if method_call.ident.name == method && args.len() > pos { if method_call.ident.name.as_str() == method && args.len() > pos {
lint_single_char_pattern(cx, expr, &args[pos]); lint_single_char_pattern(cx, expr, &args[pos]);
} }
} }
@@ -945,7 +944,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
if in_external_macro(cx.sess(), implitem.span) { if in_external_macro(cx.sess(), implitem.span) {
return; return;
} }
let name = implitem.ident.name; let name = implitem.ident.name.as_str();
let parent = cx.tcx.hir().get_parent_item(implitem.hir_id); let parent = cx.tcx.hir().get_parent_item(implitem.hir_id);
let item = cx.tcx.hir().expect_item_by_hir_id(parent); let item = cx.tcx.hir().expect_item_by_hir_id(parent);
let def_id = cx.tcx.hir().local_def_id_from_hir_id(item.hir_id); let def_id = cx.tcx.hir().local_def_id_from_hir_id(item.hir_id);
@@ -958,7 +957,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
then { then {
if cx.access_levels.is_exported(implitem.hir_id) { if cx.access_levels.is_exported(implitem.hir_id) {
// check missing trait implementations // check missing trait implementations
for &(method_name, n_args, self_kind, out_type, trait_name) in TRAIT_METHODS.iter() { for &(method_name, n_args, self_kind, out_type, trait_name) in &TRAIT_METHODS {
if name == method_name && if name == method_name &&
sig.decl.inputs.len() == n_args && sig.decl.inputs.len() == n_args &&
out_type.matches(cx, &sig.decl.output) && out_type.matches(cx, &sig.decl.output) &&
@@ -973,7 +972,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
// check conventions w.r.t. conversion method names and predicates // check conventions w.r.t. conversion method names and predicates
let is_copy = is_copy(cx, ty); let is_copy = is_copy(cx, ty);
for &(ref conv, self_kinds) in &CONVENTIONS { for &(ref conv, self_kinds) in &CONVENTIONS {
if conv.check(&name.as_str()) { if conv.check(&name) {
if !self_kinds if !self_kinds
.iter() .iter()
.any(|k| k.matches(cx, first_arg_ty, first_arg, self_ty, is_copy, &implitem.generics)) { .any(|k| k.matches(cx, first_arg_ty, first_arg, self_ty, is_copy, &implitem.generics)) {
@@ -1032,7 +1031,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
} }
} }
if name == sym!(new) && !same_tys(cx, ret_ty, ty) { if name == "new" && !same_tys(cx, ret_ty, ty) {
span_lint( span_lint(
cx, cx,
NEW_RET_NO_SELF, NEW_RET_NO_SELF,
@@ -2407,63 +2406,59 @@ const CONVENTIONS: [(Convention, &[SelfKind]); 7] = [
]; ];
#[rustfmt::skip] #[rustfmt::skip]
lazy_static! { const TRAIT_METHODS: [(&str, usize, SelfKind, OutType, &str); 30] = [
static ref TRAIT_METHODS: [(Symbol, usize, SelfKind, OutType, &'static str); 30] = [ ("add", 2, SelfKind::Value, OutType::Any, "std::ops::Add"),
(sym!(add), 2, SelfKind::Value, OutType::Any, "std::ops::Add"), ("as_mut", 1, SelfKind::RefMut, OutType::Ref, "std::convert::AsMut"),
(sym!(as_mut), 1, SelfKind::RefMut, OutType::Ref, "std::convert::AsMut"), ("as_ref", 1, SelfKind::Ref, OutType::Ref, "std::convert::AsRef"),
(sym!(as_ref), 1, SelfKind::Ref, OutType::Ref, "std::convert::AsRef"), ("bitand", 2, SelfKind::Value, OutType::Any, "std::ops::BitAnd"),
(sym!(bitand), 2, SelfKind::Value, OutType::Any, "std::ops::BitAnd"), ("bitor", 2, SelfKind::Value, OutType::Any, "std::ops::BitOr"),
(sym!(bitor), 2, SelfKind::Value, OutType::Any, "std::ops::BitOr"), ("bitxor", 2, SelfKind::Value, OutType::Any, "std::ops::BitXor"),
(sym!(bitxor), 2, SelfKind::Value, OutType::Any, "std::ops::BitXor"), ("borrow", 1, SelfKind::Ref, OutType::Ref, "std::borrow::Borrow"),
(sym!(borrow), 1, SelfKind::Ref, OutType::Ref, "std::borrow::Borrow"), ("borrow_mut", 1, SelfKind::RefMut, OutType::Ref, "std::borrow::BorrowMut"),
(sym!(borrow_mut), 1, SelfKind::RefMut, OutType::Ref, "std::borrow::BorrowMut"), ("clone", 1, SelfKind::Ref, OutType::Any, "std::clone::Clone"),
(sym!(clone), 1, SelfKind::Ref, OutType::Any, "std::clone::Clone"), ("cmp", 2, SelfKind::Ref, OutType::Any, "std::cmp::Ord"),
(sym!(cmp), 2, SelfKind::Ref, OutType::Any, "std::cmp::Ord"), ("default", 0, SelfKind::No, OutType::Any, "std::default::Default"),
(sym!(default), 0, SelfKind::No, OutType::Any, "std::default::Default"), ("deref", 1, SelfKind::Ref, OutType::Ref, "std::ops::Deref"),
(sym!(deref), 1, SelfKind::Ref, OutType::Ref, "std::ops::Deref"), ("deref_mut", 1, SelfKind::RefMut, OutType::Ref, "std::ops::DerefMut"),
(sym!(deref_mut), 1, SelfKind::RefMut, OutType::Ref, "std::ops::DerefMut"), ("div", 2, SelfKind::Value, OutType::Any, "std::ops::Div"),
(sym!(div), 2, SelfKind::Value, OutType::Any, "std::ops::Div"), ("drop", 1, SelfKind::RefMut, OutType::Unit, "std::ops::Drop"),
(sym!(drop), 1, SelfKind::RefMut, OutType::Unit, "std::ops::Drop"), ("eq", 2, SelfKind::Ref, OutType::Bool, "std::cmp::PartialEq"),
(sym!(eq), 2, SelfKind::Ref, OutType::Bool, "std::cmp::PartialEq"), ("from_iter", 1, SelfKind::No, OutType::Any, "std::iter::FromIterator"),
(sym!(from_iter), 1, SelfKind::No, OutType::Any, "std::iter::FromIterator"), ("from_str", 1, SelfKind::No, OutType::Any, "std::str::FromStr"),
(sym!(from_str), 1, SelfKind::No, OutType::Any, "std::str::FromStr"), ("hash", 2, SelfKind::Ref, OutType::Unit, "std::hash::Hash"),
(sym!(hash), 2, SelfKind::Ref, OutType::Unit, "std::hash::Hash"), ("index", 2, SelfKind::Ref, OutType::Ref, "std::ops::Index"),
(sym!(index), 2, SelfKind::Ref, OutType::Ref, "std::ops::Index"), ("index_mut", 2, SelfKind::RefMut, OutType::Ref, "std::ops::IndexMut"),
(sym!(index_mut), 2, SelfKind::RefMut, OutType::Ref, "std::ops::IndexMut"), ("into_iter", 1, SelfKind::Value, OutType::Any, "std::iter::IntoIterator"),
(sym!(into_iter), 1, SelfKind::Value, OutType::Any, "std::iter::IntoIterator"), ("mul", 2, SelfKind::Value, OutType::Any, "std::ops::Mul"),
(sym!(mul), 2, SelfKind::Value, OutType::Any, "std::ops::Mul"), ("neg", 1, SelfKind::Value, OutType::Any, "std::ops::Neg"),
(sym!(neg), 1, SelfKind::Value, OutType::Any, "std::ops::Neg"), ("next", 1, SelfKind::RefMut, OutType::Any, "std::iter::Iterator"),
(sym!(next), 1, SelfKind::RefMut, OutType::Any, "std::iter::Iterator"), ("not", 1, SelfKind::Value, OutType::Any, "std::ops::Not"),
(sym!(not), 1, SelfKind::Value, OutType::Any, "std::ops::Not"), ("rem", 2, SelfKind::Value, OutType::Any, "std::ops::Rem"),
(sym!(rem), 2, SelfKind::Value, OutType::Any, "std::ops::Rem"), ("shl", 2, SelfKind::Value, OutType::Any, "std::ops::Shl"),
(sym!(shl), 2, SelfKind::Value, OutType::Any, "std::ops::Shl"), ("shr", 2, SelfKind::Value, OutType::Any, "std::ops::Shr"),
(sym!(shr), 2, SelfKind::Value, OutType::Any, "std::ops::Shr"), ("sub", 2, SelfKind::Value, OutType::Any, "std::ops::Sub"),
(sym!(sub), 2, SelfKind::Value, OutType::Any, "std::ops::Sub"),
]; ];
}
#[rustfmt::skip] #[rustfmt::skip]
lazy_static! { const PATTERN_METHODS: [(&str, usize); 17] = [
static ref PATTERN_METHODS: [(Symbol, usize); 17] = [ ("contains", 1),
(sym!(contains), 1), ("starts_with", 1),
(sym!(starts_with), 1), ("ends_with", 1),
(sym!(ends_with), 1), ("find", 1),
(sym!(find), 1), ("rfind", 1),
(sym!(rfind), 1), ("split", 1),
(sym!(split), 1), ("rsplit", 1),
(sym!(rsplit), 1), ("split_terminator", 1),
(sym!(split_terminator), 1), ("rsplit_terminator", 1),
(sym!(rsplit_terminator), 1), ("splitn", 2),
(sym!(splitn), 2), ("rsplitn", 2),
(sym!(rsplitn), 2), ("matches", 1),
(sym!(matches), 1), ("rmatches", 1),
(sym!(rmatches), 1), ("match_indices", 1),
(sym!(match_indices), 1), ("rmatch_indices", 1),
(sym!(rmatch_indices), 1), ("trim_start_matches", 1),
(sym!(trim_start_matches), 1), ("trim_end_matches", 1),
(sym!(trim_end_matches), 1),
]; ];
}
#[derive(Clone, Copy, PartialEq, Debug)] #[derive(Clone, Copy, PartialEq, Debug)]
enum SelfKind { enum SelfKind {

View File

@@ -37,7 +37,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ReplaceConsts {
if let hir::ExprKind::Path(ref qp) = expr.node; if let hir::ExprKind::Path(ref qp) = expr.node;
if let Res::Def(DefKind::Const, def_id) = cx.tables.qpath_res(qp, expr.hir_id); if let Res::Def(DefKind::Const, def_id) = cx.tables.qpath_res(qp, expr.hir_id);
then { then {
for (const_path, repl_snip) in REPLACEMENTS.iter() { for (const_path, repl_snip) in &REPLACEMENTS {
if match_def_path(cx, def_id, const_path) { if match_def_path(cx, def_id, const_path) {
span_lint_and_sugg( span_lint_and_sugg(
cx, cx,

View File

@@ -7,8 +7,9 @@ use rustc::hir::*;
use rustc::lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintArray, LintPass}; use rustc::lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintArray, LintPass};
use rustc::{declare_lint_pass, declare_tool_lint, impl_lint_pass}; use rustc::{declare_lint_pass, declare_tool_lint, impl_lint_pass};
use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use syntax::ast::{Crate as AstCrate, Name}; use syntax::ast::{Crate as AstCrate, ItemKind, Name};
use syntax::source_map::Span; use syntax::source_map::Span;
use syntax_pos::symbol::LocalInternedString;
declare_clippy_lint! { declare_clippy_lint! {
/// **What it does:** Checks for various things we like to keep tidy in clippy. /// **What it does:** Checks for various things we like to keep tidy in clippy.
@@ -74,12 +75,15 @@ declare_clippy_lint! {
declare_lint_pass!(ClippyLintsInternal => [CLIPPY_LINTS_INTERNAL]); declare_lint_pass!(ClippyLintsInternal => [CLIPPY_LINTS_INTERNAL]);
impl EarlyLintPass for ClippyLintsInternal { impl EarlyLintPass for ClippyLintsInternal {
fn check_crate(&mut self, _cx: &EarlyContext<'_>, _krate: &AstCrate) { fn check_crate(&mut self, cx: &EarlyContext<'_>, krate: &AstCrate) {
/* if let Some(utils) = krate
FIXME: turn back on when we get rid of all the lazy_statics .module
if let Some(utils) = krate.module.items.iter().find(|item| item.ident.name == sym!(utils)) { .items
.iter()
.find(|item| item.ident.name.as_str() == "utils")
{
if let ItemKind::Mod(ref utils_mod) = utils.node { if let ItemKind::Mod(ref utils_mod) = utils.node {
if let Some(paths) = utils_mod.items.iter().find(|item| item.ident.name == sym!(paths)) { if let Some(paths) = utils_mod.items.iter().find(|item| item.ident.name.as_str() == "paths") {
if let ItemKind::Mod(ref paths_mod) = paths.node { if let ItemKind::Mod(ref paths_mod) = paths.node {
let mut last_name: Option<LocalInternedString> = None; let mut last_name: Option<LocalInternedString> = None;
for item in &*paths_mod.items { for item in &*paths_mod.items {
@@ -101,7 +105,6 @@ impl EarlyLintPass for ClippyLintsInternal {
} }
} }
} }
*/
} }
} }

View File

@@ -96,10 +96,10 @@ pub const RESULT_OK: [&str; 4] = ["core", "result", "Result", "Ok"];
pub const SERDE_DE_VISITOR: [&str; 3] = ["serde", "de", "Visitor"]; pub const SERDE_DE_VISITOR: [&str; 3] = ["serde", "de", "Visitor"];
pub const SLICE_INTO_VEC: [&str; 4] = ["alloc", "slice", "<impl [T]>", "into_vec"]; pub const SLICE_INTO_VEC: [&str; 4] = ["alloc", "slice", "<impl [T]>", "into_vec"];
pub const SLICE_ITER: [&str; 3] = ["core", "slice", "Iter"]; pub const SLICE_ITER: [&str; 3] = ["core", "slice", "Iter"];
pub const STD_MEM_TRANSMUTE: [&str; 3] = ["std", "mem", "transmute"];
pub const STD_PTR_NULL: [&str; 3] = ["std", "ptr", "null"];
pub const STDERR: [&str; 4] = ["std", "io", "stdio", "stderr"]; pub const STDERR: [&str; 4] = ["std", "io", "stdio", "stderr"];
pub const STDOUT: [&str; 4] = ["std", "io", "stdio", "stdout"]; pub const STDOUT: [&str; 4] = ["std", "io", "stdio", "stdout"];
pub const STD_MEM_TRANSMUTE: [&str; 3] = ["std", "mem", "transmute"];
pub const STD_PTR_NULL: [&str; 3] = ["std", "ptr", "null"];
pub const STRING: [&str; 3] = ["alloc", "string", "String"]; pub const STRING: [&str; 3] = ["alloc", "string", "String"];
pub const TO_OWNED: [&str; 3] = ["alloc", "borrow", "ToOwned"]; pub const TO_OWNED: [&str; 3] = ["alloc", "borrow", "ToOwned"];
pub const TO_OWNED_METHOD: [&str; 4] = ["alloc", "borrow", "ToOwned", "to_owned"]; pub const TO_OWNED_METHOD: [&str; 4] = ["alloc", "borrow", "ToOwned", "to_owned"];