Fix symbol string comparison dogfood

This commit is contained in:
Cameron Steffen 2020-12-29 16:04:31 -06:00
parent cc26919b4d
commit 7871ebaab9
18 changed files with 34 additions and 32 deletions

View File

@ -399,7 +399,7 @@ fn extract_clippy_lint(lint: &NestedMetaItem) -> Option<SymbolStr> {
if let Some(meta_item) = lint.meta_item(); if let Some(meta_item) = lint.meta_item();
if meta_item.path.segments.len() > 1; if meta_item.path.segments.len() > 1;
if let tool_name = meta_item.path.segments[0].ident; if let tool_name = meta_item.path.segments[0].ident;
if tool_name.as_str() == "clippy"; if tool_name.name == sym::clippy;
let lint_name = meta_item.path.segments.last().unwrap().ident.name; let lint_name = meta_item.path.segments.last().unwrap().ident.name;
then { then {
return Some(lint_name.as_str()); return Some(lint_name.as_str());

View File

@ -145,7 +145,7 @@ impl<'tcx, 'l> ArmVisitor<'tcx, 'l> {
fn is_mutex_lock_call<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<&'tcx Expr<'tcx>> { fn is_mutex_lock_call<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<&'tcx Expr<'tcx>> {
if_chain! { if_chain! {
if let ExprKind::MethodCall(path, _span, args, _) = &expr.kind; if let ExprKind::MethodCall(path, _span, args, _) = &expr.kind;
if path.ident.to_string() == "lock"; if path.ident.as_str() == "lock";
let ty = cx.typeck_results().expr_ty(&args[0]); let ty = cx.typeck_results().expr_ty(&args[0]);
if is_type_diagnostic_item(cx, ty, sym!(mutex_type)); if is_type_diagnostic_item(cx, ty, sym!(mutex_type));
then { then {

View File

@ -9,7 +9,7 @@ use rustc_hir::{
}; };
use rustc_lint::{LateContext, LateLintPass}; use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::Span; use rustc_span::{sym, Span};
declare_clippy_lint! { declare_clippy_lint! {
/// **What it does:** It checks for manual implementations of `async` functions. /// **What it does:** It checks for manual implementations of `async` functions.
@ -137,7 +137,7 @@ fn future_output_ty<'tcx>(trait_ref: &'tcx TraitRef<'tcx>) -> Option<&'tcx Ty<'t
if let Some(args) = segment.args; if let Some(args) = segment.args;
if args.bindings.len() == 1; if args.bindings.len() == 1;
let binding = &args.bindings[0]; let binding = &args.bindings[0];
if binding.ident.as_str() == "Output"; if binding.ident.name == sym::Output;
if let TypeBindingKind::Equality{ty: output} = binding.kind; if let TypeBindingKind::Equality{ty: output} = binding.kind;
then { then {
return Some(output) return Some(output)

View File

@ -53,7 +53,7 @@ impl<'tcx> LateLintPass<'tcx> for MapClone {
if_chain! { if_chain! {
if let hir::ExprKind::MethodCall(ref method, _, ref args, _) = e.kind; if let hir::ExprKind::MethodCall(ref method, _, ref args, _) = e.kind;
if args.len() == 2; if args.len() == 2;
if method.ident.as_str() == "map"; if method.ident.name == sym::map;
let ty = cx.typeck_results().expr_ty(&args[0]); let ty = cx.typeck_results().expr_ty(&args[0]);
if is_type_diagnostic_item(cx, ty, sym::option_type) || match_trait_method(cx, e, &paths::ITERATOR); if is_type_diagnostic_item(cx, ty, sym::option_type) || match_trait_method(cx, e, &paths::ITERATOR);
if let hir::ExprKind::Closure(_, _, body_id, _, _) = args[1].kind; if let hir::ExprKind::Closure(_, _, body_id, _, _) = args[1].kind;

View File

@ -63,7 +63,7 @@ impl<'tcx> LateLintPass<'tcx> for MapIdentity {
fn get_map_argument<'a>(cx: &LateContext<'_>, expr: &'a Expr<'a>) -> Option<&'a [Expr<'a>]> { fn get_map_argument<'a>(cx: &LateContext<'_>, expr: &'a Expr<'a>) -> Option<&'a [Expr<'a>]> {
if_chain! { if_chain! {
if let ExprKind::MethodCall(ref method, _, ref args, _) = expr.kind; if let ExprKind::MethodCall(ref method, _, ref args, _) = expr.kind;
if args.len() == 2 && method.ident.as_str() == "map"; if args.len() == 2 && method.ident.name == sym::map;
let caller_ty = cx.typeck_results().expr_ty(&args[0]); let caller_ty = cx.typeck_results().expr_ty(&args[0]);
if match_trait_method(cx, expr, &paths::ITERATOR) if match_trait_method(cx, expr, &paths::ITERATOR)
|| is_type_diagnostic_item(cx, caller_ty, sym::result_type) || is_type_diagnostic_item(cx, caller_ty, sym::result_type)

View File

@ -3095,7 +3095,7 @@ fn lint_flat_map_identity<'tcx>(
if let hir::ExprKind::Path(hir::QPath::Resolved(_, ref path)) = body.value.kind; if let hir::ExprKind::Path(hir::QPath::Resolved(_, ref path)) = body.value.kind;
if path.segments.len() == 1; if path.segments.len() == 1;
if path.segments[0].ident.as_str() == binding_ident.as_str(); if path.segments[0].ident.name == binding_ident.name;
then { then {
apply_lint("called `flat_map(|x| x)` on an `Iterator`"); apply_lint("called `flat_map(|x| x)` on an `Iterator`");

View File

@ -89,9 +89,9 @@ fn min_max<'a>(cx: &LateContext<'_>, expr: &'a Expr<'a>) -> Option<(MinMax, Cons
if let [obj, _] = args; if let [obj, _] = args;
if cx.typeck_results().expr_ty(obj).is_floating_point() || match_trait_method(cx, expr, &paths::ORD); if cx.typeck_results().expr_ty(obj).is_floating_point() || match_trait_method(cx, expr, &paths::ORD);
then { then {
if path.ident.as_str() == sym!(max).as_str() { if path.ident.name == sym!(max) {
fetch_const(cx, args, MinMax::Max) fetch_const(cx, args, MinMax::Max)
} else if path.ident.as_str() == sym!(min).as_str() { } else if path.ident.name == sym!(min) {
fetch_const(cx, args, MinMax::Min) fetch_const(cx, args, MinMax::Min)
} else { } else {
None None

View File

@ -63,7 +63,7 @@ impl MissingDoc {
if let Some(meta) = list.get(0); if let Some(meta) = list.get(0);
if let Some(name) = meta.ident(); if let Some(name) = meta.ident();
then { then {
name.as_str() == "include" name.name == sym::include
} else { } else {
false false
} }

View File

@ -13,6 +13,7 @@ use rustc_infer::infer::TyCtxtInferExt;
use rustc_lint::{LateContext, LateLintPass}; use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::{self, TypeFoldable}; use rustc_middle::ty::{self, TypeFoldable};
use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::symbol::kw;
use rustc_span::{sym, Span}; use rustc_span::{sym, Span};
use rustc_target::spec::abi::Abi; use rustc_target::spec::abi::Abi;
use rustc_trait_selection::traits; use rustc_trait_selection::traits;
@ -153,7 +154,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
// Ignore `self`s. // Ignore `self`s.
if idx == 0 { if idx == 0 {
if let PatKind::Binding(.., ident, _) = arg.pat.kind { if let PatKind::Binding(.., ident, _) = arg.pat.kind {
if ident.as_str() == "self" { if ident.name == kw::SelfLower {
continue; continue;
} }
} }

View File

@ -66,7 +66,7 @@ declare_lint_pass!(OptionIfLetElse => [OPTION_IF_LET_ELSE]);
/// Returns true iff the given expression is the result of calling `Result::ok` /// Returns true iff the given expression is the result of calling `Result::ok`
fn is_result_ok(cx: &LateContext<'_>, expr: &'_ Expr<'_>) -> bool { fn is_result_ok(cx: &LateContext<'_>, expr: &'_ Expr<'_>) -> bool {
if let ExprKind::MethodCall(ref path, _, &[ref receiver], _) = &expr.kind { if let ExprKind::MethodCall(ref path, _, &[ref receiver], _) = &expr.kind {
path.ident.name.to_ident_string() == "ok" path.ident.name.as_str() == "ok"
&& is_type_diagnostic_item(cx, &cx.typeck_results().expr_ty(&receiver), sym::result_type) && is_type_diagnostic_item(cx, &cx.typeck_results().expr_ty(&receiver), sym::result_type)
} else { } else {
false false

View File

@ -389,5 +389,5 @@ fn is_self_shadow(name: Symbol, expr: &Expr<'_>) -> bool {
} }
fn path_eq_name(name: Symbol, path: &Path<'_>) -> bool { fn path_eq_name(name: Symbol, path: &Path<'_>) -> bool {
!path.is_global() && path.segments.len() == 1 && path.segments[0].ident.as_str() == name.as_str() !path.is_global() && path.segments.len() == 1 && path.segments[0].ident.name == name
} }

View File

@ -91,7 +91,7 @@ fn check_manual_swap(cx: &LateContext<'_>, block: &Block<'_>) {
if let ExprKind::Path(QPath::Resolved(None, ref rhs2)) = rhs2.kind; if let ExprKind::Path(QPath::Resolved(None, ref rhs2)) = rhs2.kind;
if rhs2.segments.len() == 1; if rhs2.segments.len() == 1;
if ident.as_str() == rhs2.segments[0].ident.as_str(); if ident.name == rhs2.segments[0].ident.name;
if eq_expr_value(cx, tmp_init, lhs1); if eq_expr_value(cx, tmp_init, lhs1);
if eq_expr_value(cx, rhs1, lhs2); if eq_expr_value(cx, rhs1, lhs2);
then { then {

View File

@ -183,7 +183,7 @@ fn detect_lint(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<LintTrigger> {
Param { pat: Pat { kind: PatKind::Binding(_, _, right_ident, _), .. }, .. } Param { pat: Pat { kind: PatKind::Binding(_, _, right_ident, _), .. }, .. }
] = &closure_body.params; ] = &closure_body.params;
if let ExprKind::MethodCall(method_path, _, [ref left_expr, ref right_expr], _) = &closure_body.value.kind; if let ExprKind::MethodCall(method_path, _, [ref left_expr, ref right_expr], _) = &closure_body.value.kind;
if method_path.ident.name.to_ident_string() == "cmp"; if method_path.ident.name == sym::cmp;
then { then {
let (closure_body, closure_arg, reverse) = if mirrored_exprs( let (closure_body, closure_arg, reverse) = if mirrored_exprs(
&cx, &cx,

View File

@ -80,10 +80,10 @@ impl<'tcx> LateLintPass<'tcx> for UselessConversion {
); );
} }
} }
if match_trait_method(cx, e, &paths::INTO_ITERATOR) && &*name.ident.as_str() == "into_iter" { if match_trait_method(cx, e, &paths::INTO_ITERATOR) && name.ident.name == sym::into_iter {
if let Some(parent_expr) = get_parent_expr(cx, e) { if let Some(parent_expr) = get_parent_expr(cx, e) {
if let ExprKind::MethodCall(ref parent_name, ..) = parent_expr.kind { if let ExprKind::MethodCall(ref parent_name, ..) = parent_expr.kind {
if &*parent_name.ident.as_str() != "into_iter" { if parent_name.ident.name != sym::into_iter {
return; return;
} }
} }

View File

@ -1,6 +1,7 @@
use rustc_ast::ast; use rustc_ast::ast;
use rustc_errors::Applicability; use rustc_errors::Applicability;
use rustc_session::Session; use rustc_session::Session;
use rustc_span::sym;
use std::str::FromStr; use std::str::FromStr;
/// Deprecation status of attributes known by Clippy. /// Deprecation status of attributes known by Clippy.
@ -64,11 +65,11 @@ pub fn get_attr<'a>(
return false; return false;
}; };
let attr_segments = &attr.path.segments; let attr_segments = &attr.path.segments;
if attr_segments.len() == 2 && attr_segments[0].ident.to_string() == "clippy" { if attr_segments.len() == 2 && attr_segments[0].ident.name == sym::clippy {
BUILTIN_ATTRIBUTES BUILTIN_ATTRIBUTES
.iter() .iter()
.find_map(|(builtin_name, deprecation_status)| { .find_map(|&(builtin_name, ref deprecation_status)| {
if *builtin_name == attr_segments[1].ident.to_string() { if attr_segments[1].ident.name.as_str() == builtin_name {
Some(deprecation_status) Some(deprecation_status)
} else { } else {
None None
@ -99,7 +100,7 @@ pub fn get_attr<'a>(
}, },
DeprecationStatus::None => { DeprecationStatus::None => {
diag.cancel(); diag.cancel();
attr_segments[1].ident.to_string() == name attr_segments[1].ident.name.as_str() == name
}, },
} }
}, },

View File

@ -86,7 +86,7 @@ impl<'a, 'tcx> SpanlessEq<'a, 'tcx> {
lb == rb && l_mut == r_mut && self.eq_expr(le, re) lb == rb && l_mut == r_mut && self.eq_expr(le, re)
}, },
(&ExprKind::Continue(li), &ExprKind::Continue(ri)) => { (&ExprKind::Continue(li), &ExprKind::Continue(ri)) => {
both(&li.label, &ri.label, |l, r| l.ident.as_str() == r.ident.as_str()) both(&li.label, &ri.label, |l, r| l.ident.name == r.ident.name)
}, },
(&ExprKind::Assign(ref ll, ref lr, _), &ExprKind::Assign(ref rl, ref rr, _)) => { (&ExprKind::Assign(ref ll, ref lr, _), &ExprKind::Assign(ref rl, ref rr, _)) => {
self.allow_side_effects && self.eq_expr(ll, rl) && self.eq_expr(lr, rr) self.allow_side_effects && self.eq_expr(ll, rl) && self.eq_expr(lr, rr)
@ -102,7 +102,7 @@ impl<'a, 'tcx> SpanlessEq<'a, 'tcx> {
}) })
}, },
(&ExprKind::Break(li, ref le), &ExprKind::Break(ri, ref re)) => { (&ExprKind::Break(li, ref le), &ExprKind::Break(ri, ref re)) => {
both(&li.label, &ri.label, |l, r| l.ident.as_str() == r.ident.as_str()) both(&li.label, &ri.label, |l, r| l.ident.name == r.ident.name)
&& both(le, re, |l, r| self.eq_expr(l, r)) && both(le, re, |l, r| self.eq_expr(l, r))
}, },
(&ExprKind::Box(ref l), &ExprKind::Box(ref r)) => self.eq_expr(l, r), (&ExprKind::Box(ref l), &ExprKind::Box(ref r)) => self.eq_expr(l, r),
@ -121,7 +121,7 @@ impl<'a, 'tcx> SpanlessEq<'a, 'tcx> {
}, },
(&ExprKind::Lit(ref l), &ExprKind::Lit(ref r)) => l.node == r.node, (&ExprKind::Lit(ref l), &ExprKind::Lit(ref r)) => l.node == r.node,
(&ExprKind::Loop(ref lb, ref ll, ref lls), &ExprKind::Loop(ref rb, ref rl, ref rls)) => { (&ExprKind::Loop(ref lb, ref ll, ref lls), &ExprKind::Loop(ref rb, ref rl, ref rls)) => {
lls == rls && self.eq_block(lb, rb) && both(ll, rl, |l, r| l.ident.as_str() == r.ident.as_str()) lls == rls && self.eq_block(lb, rb) && both(ll, rl, |l, r| l.ident.name == r.ident.name)
}, },
(&ExprKind::Match(ref le, ref la, ref ls), &ExprKind::Match(ref re, ref ra, ref rs)) => { (&ExprKind::Match(ref le, ref la, ref ls), &ExprKind::Match(ref re, ref ra, ref rs)) => {
ls == rs ls == rs
@ -188,7 +188,7 @@ impl<'a, 'tcx> SpanlessEq<'a, 'tcx> {
pub fn eq_fieldpat(&mut self, left: &FieldPat<'_>, right: &FieldPat<'_>) -> bool { pub fn eq_fieldpat(&mut self, left: &FieldPat<'_>, right: &FieldPat<'_>) -> bool {
let (FieldPat { ident: li, pat: lp, .. }, FieldPat { ident: ri, pat: rp, .. }) = (&left, &right); let (FieldPat { ident: li, pat: lp, .. }, FieldPat { ident: ri, pat: rp, .. }) = (&left, &right);
li.name.as_str() == ri.name.as_str() && self.eq_pat(lp, rp) li.name == ri.name && self.eq_pat(lp, rp)
} }
/// Checks whether two patterns are the same. /// Checks whether two patterns are the same.
@ -202,7 +202,7 @@ impl<'a, 'tcx> SpanlessEq<'a, 'tcx> {
self.eq_qpath(lp, rp) && over(la, ra, |l, r| self.eq_pat(l, r)) && ls == rs self.eq_qpath(lp, rp) && over(la, ra, |l, r| self.eq_pat(l, r)) && ls == rs
}, },
(&PatKind::Binding(ref lb, .., ref li, ref lp), &PatKind::Binding(ref rb, .., ref ri, ref rp)) => { (&PatKind::Binding(ref lb, .., ref li, ref lp), &PatKind::Binding(ref rb, .., ref ri, ref rp)) => {
lb == rb && li.name.as_str() == ri.name.as_str() && both(lp, rp, |l, r| self.eq_pat(l, r)) lb == rb && li.name == ri.name && both(lp, rp, |l, r| self.eq_pat(l, r))
}, },
(&PatKind::Path(ref l), &PatKind::Path(ref r)) => self.eq_qpath(l, r), (&PatKind::Path(ref l), &PatKind::Path(ref r)) => self.eq_qpath(l, r),
(&PatKind::Lit(ref l), &PatKind::Lit(ref r)) => self.eq_expr(l, r), (&PatKind::Lit(ref l), &PatKind::Lit(ref r)) => self.eq_expr(l, r),
@ -263,8 +263,7 @@ impl<'a, 'tcx> SpanlessEq<'a, 'tcx> {
pub fn eq_path_segment(&mut self, left: &PathSegment<'_>, right: &PathSegment<'_>) -> bool { pub fn eq_path_segment(&mut self, left: &PathSegment<'_>, right: &PathSegment<'_>) -> bool {
// The == of idents doesn't work with different contexts, // The == of idents doesn't work with different contexts,
// we have to be explicit about hygiene // we have to be explicit about hygiene
left.ident.as_str() == right.ident.as_str() left.ident.name == right.ident.name && both(&left.args, &right.args, |l, r| self.eq_path_parameters(l, r))
&& both(&left.args, &right.args, |l, r| self.eq_path_parameters(l, r))
} }
pub fn eq_ty(&mut self, left: &Ty<'_>, right: &Ty<'_>) -> bool { pub fn eq_ty(&mut self, left: &Ty<'_>, right: &Ty<'_>) -> bool {

View File

@ -158,7 +158,7 @@ fn get_vec_init_kind<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> Op
ExprKind::Path(QPath::TypeRelative(ty, name)) ExprKind::Path(QPath::TypeRelative(ty, name))
if is_type_diagnostic_item(cx, cx.typeck_results().node_type(ty.hir_id), sym::vec_type) => if is_type_diagnostic_item(cx, cx.typeck_results().node_type(ty.hir_id), sym::vec_type) =>
{ {
if name.ident.name.as_str() == "new" { if name.ident.name == sym::new {
return Some(VecInitKind::New); return Some(VecInitKind::New);
} else if name.ident.name.as_str() == "with_capacity" { } else if name.ident.name.as_str() == "with_capacity" {
return args.get(0).and_then(|arg| { return args.get(0).and_then(|arg| {

View File

@ -7,7 +7,8 @@ use rustc_hir::{
}; };
use rustc_lint::{LateContext, LateLintPass}; use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::BytePos; use rustc_span::symbol::kw;
use rustc_span::{sym, BytePos};
declare_clippy_lint! { declare_clippy_lint! {
/// **What it does:** Checks for `use Enum::*`. /// **What it does:** Checks for `use Enum::*`.
@ -198,12 +199,12 @@ impl WildcardImports {
// Allow "...prelude::..::*" imports. // Allow "...prelude::..::*" imports.
// Many crates have a prelude, and it is imported as a glob by design. // Many crates have a prelude, and it is imported as a glob by design.
fn is_prelude_import(segments: &[PathSegment<'_>]) -> bool { fn is_prelude_import(segments: &[PathSegment<'_>]) -> bool {
segments.iter().any(|ps| ps.ident.as_str() == "prelude") segments.iter().any(|ps| ps.ident.name == sym::prelude)
} }
// Allow "super::*" imports in tests. // Allow "super::*" imports in tests.
fn is_super_only_import(segments: &[PathSegment<'_>]) -> bool { fn is_super_only_import(segments: &[PathSegment<'_>]) -> bool {
segments.len() == 1 && segments[0].ident.as_str() == "super" segments.len() == 1 && segments[0].ident.name == kw::Super
} }
fn is_test_module_or_function(item: &Item<'_>) -> bool { fn is_test_module_or_function(item: &Item<'_>) -> bool {