Skip to content

Prevent symbocalypse #4110

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

Merged
merged 4 commits into from
May 18, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 18 additions & 23 deletions clippy_lints/src/approx_const.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use crate::utils::span_lint;
use crate::utils::sym;
use lazy_static::lazy_static;
use rustc::hir::*;
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
use rustc::{declare_lint_pass, declare_tool_lint};
use std::f64::consts as f64;
use syntax::ast::{FloatTy, LitKind};
use syntax::symbol;
use syntax::symbol::Symbol;

declare_clippy_lint! {
/// **What it does:** Checks for floating point literals that approximate
@@ -33,27 +30,25 @@ declare_clippy_lint! {
"the approximate of a known float constant (in `std::fXX::consts`)"
}

lazy_static! {
// Tuples are of the form (constant, name, min_digits)
static ref KNOWN_CONSTS: [(f64, Symbol, usize); 16] = [
(f64::E, *sym::E, 4),
(f64::FRAC_1_PI, *sym::FRAC_1_PI, 4),
(f64::FRAC_1_SQRT_2, *sym::FRAC_1_SQRT_2, 5),
(f64::FRAC_2_PI, *sym::FRAC_2_PI, 5),
(f64::FRAC_2_SQRT_PI, *sym::FRAC_2_SQRT_PI, 5),
(f64::FRAC_PI_2, *sym::FRAC_PI_2, 5),
(f64::FRAC_PI_3, *sym::FRAC_PI_3, 5),
(f64::FRAC_PI_4, *sym::FRAC_PI_4, 5),
(f64::FRAC_PI_6, *sym::FRAC_PI_6, 5),
(f64::FRAC_PI_8, *sym::FRAC_PI_8, 5),
(f64::LN_10, *sym::LN_10, 5),
(f64::LN_2, *sym::LN_2, 5),
(f64::LOG10_E, *sym::LOG10_E, 5),
(f64::LOG2_E, *sym::LOG2_E, 5),
(f64::PI, *sym::PI, 3),
(f64::SQRT_2, *sym::SQRT_2, 5),
const KNOWN_CONSTS: [(f64, &str, usize); 16] = [
(f64::E, "E", 4),
(f64::FRAC_1_PI, "FRAC_1_PI", 4),
(f64::FRAC_1_SQRT_2, "FRAC_1_SQRT_2", 5),
(f64::FRAC_2_PI, "FRAC_2_PI", 5),
(f64::FRAC_2_SQRT_PI, "FRAC_2_SQRT_PI", 5),
(f64::FRAC_PI_2, "FRAC_PI_2", 5),
(f64::FRAC_PI_3, "FRAC_PI_3", 5),
(f64::FRAC_PI_4, "FRAC_PI_4", 5),
(f64::FRAC_PI_6, "FRAC_PI_6", 5),
(f64::FRAC_PI_8, "FRAC_PI_8", 5),
(f64::LN_10, "LN_10", 5),
(f64::LN_2, "LN_2", 5),
(f64::LOG10_E, "LOG10_E", 5),
(f64::LOG2_E, "LOG2_E", 5),
(f64::PI, "PI", 3),
(f64::SQRT_2, "SQRT_2", 5),
];
}

declare_lint_pass!(ApproxConstant => [APPROX_CONSTANT]);

@@ -77,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) {
let s = s.as_str();
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) {
span_lint(
cx,
5 changes: 2 additions & 3 deletions clippy_lints/src/assertions_on_constants.rs
Original file line number Diff line number Diff line change
@@ -5,7 +5,6 @@ use rustc::{declare_lint_pass, declare_tool_lint};
use syntax_pos::Span;

use crate::consts::{constant, Constant};
use crate::utils::sym;
use crate::utils::{in_macro_or_desugar, is_direct_expn_of, span_help_and_lint};

declare_clippy_lint! {
@@ -41,9 +40,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants {
!in_macro_or_desugar(span)
};
if_chain! {
if let Some(assert_span) = is_direct_expn_of(e.span, *sym::assert);
if let Some(assert_span) = is_direct_expn_of(e.span, "assert");
if !in_macro_or_desugar(assert_span)
|| is_direct_expn_of(assert_span, *sym::debug_assert)
|| is_direct_expn_of(assert_span, "debug_assert")
.map_or(false, debug_assert_not_in_macro_or_desugar);
if let ExprKind::Unary(_, ref lit) = e.node;
if let Some(bool_const) = constant(cx, cx.tables, lit);
8 changes: 2 additions & 6 deletions clippy_lints/src/assign_ops.rs
Original file line number Diff line number Diff line change
@@ -9,7 +9,6 @@ use crate::utils::{
get_trait_def_id, implements_trait, snippet_opt, span_lint_and_then, trait_ref_of_method, SpanlessEq,
};
use crate::utils::{higher, sugg};
use syntax::symbol::Symbol;

declare_clippy_lint! {
/// **What it does:** Checks for `a = a op b` or `a = b commutative_op a`
@@ -89,11 +88,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssignOps {
$($trait_name:ident),+) => {
match $op {
$(hir::BinOpKind::$trait_name => {
let [krate, module] = *crate::utils::paths::OPS_MODULE;
let ident = {
*crate::utils::sym::assign::$trait_name
};
let path: [Symbol; 3] = [krate, module, ident];
let [krate, module] = crate::utils::paths::OPS_MODULE;
let path: [&str; 3] = [krate, module, concat!(stringify!($trait_name), "Assign")];
let trait_id = if let Some(trait_id) = get_trait_def_id($cx, &path) {
trait_id
} else {
31 changes: 15 additions & 16 deletions clippy_lints/src/attrs.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! checks for attributes
use crate::reexport::*;
use crate::utils::sym;
use crate::utils::{
in_macro_or_desugar, is_present_in_source, last_line_of_span, match_def_path, paths, snippet_opt, span_lint,
span_lint_and_sugg, span_lint_and_then, without_block_comments,
@@ -18,7 +17,7 @@ use rustc_errors::Applicability;
use semver::Version;
use syntax::ast::{AttrStyle, Attribute, Lit, LitKind, MetaItemKind, NestedMetaItem};
use syntax::source_map::Span;
use syntax::symbol::Symbol;
use syntax_pos::symbol::Symbol;

declare_clippy_lint! {
/// **What it does:** Checks for items annotated with `#[inline(always)]`,
@@ -207,14 +206,14 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Attributes {
},
_ => {},
}
if items.is_empty() || !attr.check_name(*sym::deprecated) {
if items.is_empty() || !attr.check_name(sym!(deprecated)) {
return;
}
for item in items {
if_chain! {
if let NestedMetaItem::MetaItem(mi) = &item;
if let MetaItemKind::NameValue(lit) = &mi.node;
if mi.check_name(*sym::since);
if mi.check_name(sym!(since));
then {
check_semver(cx, item.span(), lit);
}
@@ -230,7 +229,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Attributes {
}
match item.node {
ItemKind::ExternCrate(..) | ItemKind::Use(..) => {
let skip_unused_imports = item.attrs.iter().any(|attr| attr.check_name(*sym::macro_use));
let skip_unused_imports = item.attrs.iter().any(|attr| attr.check_name(sym!(macro_use)));

for attr in &item.attrs {
if in_external_macro(cx.sess(), attr.span) {
@@ -245,17 +244,17 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Attributes {
for lint in lint_list {
match item.node {
ItemKind::Use(..) => {
if is_word(lint, *sym::unused_imports)
|| is_word(lint, *sym::deprecated)
if is_word(lint, sym!(unused_imports))
|| is_word(lint, sym!(deprecated))
{
return;
}
},
ItemKind::ExternCrate(..) => {
if is_word(lint, *sym::unused_imports) && skip_unused_imports {
if is_word(lint, sym!(unused_imports)) && skip_unused_imports {
return;
}
if is_word(lint, *sym::unused_extern_crates) {
if is_word(lint, sym!(unused_extern_crates)) {
return;
}
},
@@ -399,7 +398,7 @@ fn is_relevant_expr(cx: &LateContext<'_, '_>, tables: &ty::TypeckTables<'_>, exp
ExprKind::Call(path_expr, _) => {
if let ExprKind::Path(qpath) = &path_expr.node {
if let Some(fun_id) = tables.qpath_res(qpath, path_expr.hir_id).opt_def_id() {
!match_def_path(cx, fun_id, &*paths::BEGIN_PANIC)
!match_def_path(cx, fun_id, &paths::BEGIN_PANIC)
} else {
true
}
@@ -445,10 +444,10 @@ fn check_attrs(cx: &LateContext<'_, '_>, span: Span, name: Name, attrs: &[Attrib
}

if let Some(values) = attr.meta_item_list() {
if values.len() != 1 || !attr.check_name(*sym::inline) {
if values.len() != 1 || !attr.check_name(sym!(inline)) {
continue;
}
if is_word(&values[0], *sym::always) {
if is_word(&values[0], sym!(always)) {
span_lint(
cx,
INLINE_ALWAYS,
@@ -491,16 +490,16 @@ impl EarlyLintPass for DeprecatedCfgAttribute {
fn check_attribute(&mut self, cx: &EarlyContext<'_>, attr: &Attribute) {
if_chain! {
// check cfg_attr
if attr.check_name(*sym::cfg_attr);
if attr.check_name(sym!(cfg_attr));
if let Some(items) = attr.meta_item_list();
if items.len() == 2;
// check for `rustfmt`
if let Some(feature_item) = items[0].meta_item();
if feature_item.check_name(*sym::rustfmt);
if feature_item.check_name(sym!(rustfmt));
// check for `rustfmt_skip` and `rustfmt::skip`
if let Some(skip_item) = &items[1].meta_item();
if skip_item.check_name(*sym::rustfmt_skip) ||
skip_item.path.segments.last().expect("empty path in attribute").ident.name == *sym::skip;
if skip_item.check_name(sym!(rustfmt_skip)) ||
skip_item.path.segments.last().expect("empty path in attribute").ident.name == sym!(skip);
// Only lint outer attributes, because custom inner attributes are unstable
// Tracking issue: https://github.com/rust-lang/rust/issues/54726
if let AttrStyle::Outer = attr.style;
18 changes: 5 additions & 13 deletions clippy_lints/src/booleans.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use crate::utils::sym;
use crate::utils::{
get_trait_def_id, implements_trait, in_macro, in_macro_or_desugar, match_type, paths, snippet_opt,
span_lint_and_then, SpanlessEq,
};
use lazy_static::lazy_static;
use rustc::hir::intravisit::*;
use rustc::hir::*;
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
@@ -12,7 +10,6 @@ use rustc_data_structures::thin_vec::ThinVec;
use rustc_errors::Applicability;
use syntax::ast::LitKind;
use syntax::source_map::{dummy_spanned, Span, DUMMY_SP};
use syntax::symbol::Symbol;

declare_clippy_lint! {
/// **What it does:** Checks for boolean expressions that can be written more
@@ -52,13 +49,8 @@ declare_clippy_lint! {
"boolean expressions that contain terminals which can be eliminated"
}

lazy_static! {
// For each pairs, both orders are considered.
static ref METHODS_WITH_NEGATION: [(Symbol, Symbol); 2] = [
(*sym::is_some, *sym::is_none),
(*sym::is_err, *sym::is_ok),
];
}
const METHODS_WITH_NEGATION: [(&str, &str); 2] = [("is_some", "is_none"), ("is_err", "is_ok")];

declare_lint_pass!(NonminimalBool => [NONMINIMAL_BOOL, LOGIC_BUG]);

@@ -195,16 +187,16 @@ impl<'a, 'tcx, 'v> SuggestContext<'a, 'tcx, 'v> {
},
ExprKind::MethodCall(path, _, args) if args.len() == 1 => {
let type_of_receiver = self.cx.tables.expr_ty(&args[0]);
if !match_type(self.cx, type_of_receiver, &*paths::OPTION)
&& !match_type(self.cx, type_of_receiver, &*paths::RESULT)
if !match_type(self.cx, type_of_receiver, &paths::OPTION)
&& !match_type(self.cx, type_of_receiver, &paths::RESULT)
{
return None;
}
METHODS_WITH_NEGATION
.iter()
.cloned()
.flat_map(|(a, b)| vec![(a, b), (b, a)])
.find(|&(a, _)| a == path.ident.name)
.find(|&(a, _)| a == path.ident.name.as_str())
.and_then(|(_, neg_method)| Some(format!("{}.{}()", self.snip(&args[0])?, neg_method)))
},
_ => None,
@@ -474,5 +466,5 @@ impl<'a, 'tcx> Visitor<'tcx> for NonminimalBoolVisitor<'a, 'tcx> {

fn implements_ord<'a, 'tcx>(cx: &'a LateContext<'a, 'tcx>, expr: &Expr) -> bool {
let ty = cx.tables.expr_ty(expr);
get_trait_def_id(cx, &*paths::ORD).map_or(false, |id| implements_trait(cx, ty, id, &[]))
get_trait_def_id(cx, &paths::ORD).map_or(false, |id| implements_trait(cx, ty, id, &[]))
}
9 changes: 4 additions & 5 deletions clippy_lints/src/bytecount.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::utils::sym;
use crate::utils::{
contains_name, get_pat_name, match_type, paths, single_segment_path, snippet_with_applicability,
span_lint_and_sugg, walk_ptrs_ty,
@@ -38,10 +37,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ByteCount {
fn check_expr(&mut self, cx: &LateContext<'_, '_>, expr: &Expr) {
if_chain! {
if let ExprKind::MethodCall(ref count, _, ref count_args) = expr.node;
if count.ident.name == *sym::count;
if count.ident.name == sym!(count);
if count_args.len() == 1;
if let ExprKind::MethodCall(ref filter, _, ref filter_args) = count_args[0].node;
if filter.ident.name == *sym::filter;
if filter.ident.name == sym!(filter);
if filter_args.len() == 2;
if let ExprKind::Closure(_, _, body_id, _, _) = filter_args[1].node;
then {
@@ -53,7 +52,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ByteCount {
if op.node == BinOpKind::Eq;
if match_type(cx,
walk_ptrs_ty(cx.tables.expr_ty(&filter_args[0])),
&*paths::SLICE_ITER);
&paths::SLICE_ITER);
then {
let needle = match get_path_name(l) {
Some(name) if check_arg(name, argname, r) => r,
@@ -68,7 +67,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ByteCount {
let haystack = if let ExprKind::MethodCall(ref path, _, ref args) =
filter_args[0].node {
let p = path.ident.name;
if (p == *sym::iter || p == *sym::iter_mut) && args.len() == 1 {
if (p == sym!(iter) || p == sym!(iter_mut)) && args.len() == 1 {
&args[0]
} else {
&filter_args[0]
5 changes: 2 additions & 3 deletions clippy_lints/src/cognitive_complexity.rs
Original file line number Diff line number Diff line change
@@ -9,7 +9,6 @@ use rustc::{declare_tool_lint, impl_lint_pass};
use syntax::ast::Attribute;
use syntax::source_map::Span;

use crate::utils::sym;
use crate::utils::{in_macro_or_desugar, is_allowed, match_type, paths, span_help_and_lint, LimitStack};

declare_clippy_lint! {
@@ -72,7 +71,7 @@ impl CognitiveComplexity {
..
} = helper;
let ret_ty = cx.tables.node_type(expr.hir_id);
let ret_adjust = if match_type(cx, ret_ty, &*paths::RESULT) {
let ret_adjust = if match_type(cx, ret_ty, &paths::RESULT) {
returns
} else {
returns / 2
@@ -119,7 +118,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CognitiveComplexity {
hir_id: HirId,
) {
let def_id = cx.tcx.hir().local_def_id_from_hir_id(hir_id);
if !cx.tcx.has_attr(def_id, *sym::test) {
if !cx.tcx.has_attr(def_id, sym!(test)) {
self.check(cx, body, span);
}
}
2 changes: 1 addition & 1 deletion clippy_lints/src/copy_iterator.rs
Original file line number Diff line number Diff line change
@@ -36,7 +36,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CopyIterator {
if let ItemKind::Impl(_, _, _, _, Some(ref trait_ref), _, _) = item.node {
let ty = cx.tcx.type_of(cx.tcx.hir().local_def_id_from_hir_id(item.hir_id));

if is_copy(cx, ty) && match_path(&trait_ref.path, &*paths::ITERATOR) {
if is_copy(cx, ty) && match_path(&trait_ref.path, &paths::ITERATOR) {
span_note_and_lint(
cx,
COPY_ITERATOR,
3 changes: 1 addition & 2 deletions clippy_lints/src/dbg_macro.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::utils::sym;
use crate::utils::{snippet_opt, span_help_and_lint, span_lint_and_sugg};
use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
use rustc::{declare_lint_pass, declare_tool_lint};
@@ -32,7 +31,7 @@ declare_lint_pass!(DbgMacro => [DBG_MACRO]);

impl EarlyLintPass for DbgMacro {
fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &ast::Mac) {
if mac.node.path == *sym::dbg {
if mac.node.path == sym!(dbg) {
if let Some(sugg) = tts_span(mac.node.tts.clone()).and_then(|span| snippet_opt(cx, span)) {
span_lint_and_sugg(
cx,
2 changes: 1 addition & 1 deletion clippy_lints/src/default_trait_access.rs
Original file line number Diff line number Diff line change
@@ -37,7 +37,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DefaultTraitAccess {
if !any_parent_is_automatically_derived(cx.tcx, expr.hir_id);
if let ExprKind::Path(ref qpath) = path.node;
if let Some(def_id) = cx.tables.qpath_res(qpath, path.hir_id).opt_def_id();
if match_def_path(cx, def_id, &*paths::DEFAULT_TRAIT_METHOD);
if match_def_path(cx, def_id, &paths::DEFAULT_TRAIT_METHOD);
then {
match qpath {
QPath::Resolved(..) => {
4 changes: 2 additions & 2 deletions clippy_lints/src/derive.rs
Original file line number Diff line number Diff line change
@@ -88,7 +88,7 @@ fn check_hash_peq<'a, 'tcx>(
hash_is_automatically_derived: bool,
) {
if_chain! {
if match_path(&trait_ref.path, &*paths::HASH);
if match_path(&trait_ref.path, &paths::HASH);
if let Some(peq_trait_def_id) = cx.tcx.lang_items().eq_trait();
then {
// Look for the PartialEq implementations for `ty`
@@ -129,7 +129,7 @@ fn check_hash_peq<'a, 'tcx>(

/// Implementation of the `EXPL_IMPL_CLONE_ON_COPY` lint.
fn check_copy_clone<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, item: &Item, trait_ref: &TraitRef, ty: Ty<'tcx>) {
if match_path(&trait_ref.path, &*paths::CLONE_TRAIT) {
if match_path(&trait_ref.path, &paths::CLONE_TRAIT) {
if !is_copy(cx, ty) {
return;
}
Loading