Skip to content

Commit 68bcd20

Browse files
committed
Auto merge of #6569 - camsteffen:symbol-comparison, r=Manishearth
Internal lint symbol comparisons changelog: none * Added awareness of `rustc_span::symbol::kw::*` symbols. * Compare with const symbols: `symbol.as_str() == "self"` => `symbol == kw::SelfLower` * Don't compare symbols by string: `a.as_str() == b.as_str()` => `a == b` * Lint comparing with `to_ident_string` or `to_string` instead of `Symbol::as_str`.
2 parents 2950c8e + 7871eba commit 68bcd20

28 files changed

+309
-58
lines changed

clippy_lints/src/attrs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ fn extract_clippy_lint(lint: &NestedMetaItem) -> Option<SymbolStr> {
399399
if let Some(meta_item) = lint.meta_item();
400400
if meta_item.path.segments.len() > 1;
401401
if let tool_name = meta_item.path.segments[0].ident;
402-
if tool_name.as_str() == "clippy";
402+
if tool_name.name == sym::clippy;
403403
let lint_name = meta_item.path.segments.last().unwrap().ident.name;
404404
then {
405405
return Some(lint_name.as_str());

clippy_lints/src/if_let_mutex.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ impl<'tcx, 'l> ArmVisitor<'tcx, 'l> {
145145
fn is_mutex_lock_call<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<&'tcx Expr<'tcx>> {
146146
if_chain! {
147147
if let ExprKind::MethodCall(path, _span, args, _) = &expr.kind;
148-
if path.ident.to_string() == "lock";
148+
if path.ident.as_str() == "lock";
149149
let ty = cx.typeck_results().expr_ty(&args[0]);
150150
if is_type_diagnostic_item(cx, ty, sym!(mutex_type));
151151
then {

clippy_lints/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
526526
&utils::internal_lints::OUTER_EXPN_EXPN_DATA,
527527
#[cfg(feature = "internal-lints")]
528528
&utils::internal_lints::PRODUCE_ICE,
529+
#[cfg(feature = "internal-lints")]
530+
&utils::internal_lints::UNNECESSARY_SYMBOL_STR,
529531
&approx_const::APPROX_CONSTANT,
530532
&arithmetic::FLOAT_ARITHMETIC,
531533
&arithmetic::INTEGER_ARITHMETIC,
@@ -1372,6 +1374,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
13721374
LintId::of(&utils::internal_lints::MATCH_TYPE_ON_DIAGNOSTIC_ITEM),
13731375
LintId::of(&utils::internal_lints::OUTER_EXPN_EXPN_DATA),
13741376
LintId::of(&utils::internal_lints::PRODUCE_ICE),
1377+
LintId::of(&utils::internal_lints::UNNECESSARY_SYMBOL_STR),
13751378
]);
13761379

13771380
store.register_group(true, "clippy::all", Some("clippy"), vec![

clippy_lints/src/manual_async_fn.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_hir::{
99
};
1010
use rustc_lint::{LateContext, LateLintPass};
1111
use rustc_session::{declare_lint_pass, declare_tool_lint};
12-
use rustc_span::Span;
12+
use rustc_span::{sym, Span};
1313

1414
declare_clippy_lint! {
1515
/// **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
137137
if let Some(args) = segment.args;
138138
if args.bindings.len() == 1;
139139
let binding = &args.bindings[0];
140-
if binding.ident.as_str() == "Output";
140+
if binding.ident.name == sym::Output;
141141
if let TypeBindingKind::Equality{ty: output} = binding.kind;
142142
then {
143143
return Some(output)

clippy_lints/src/map_clone.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl<'tcx> LateLintPass<'tcx> for MapClone {
5353
if_chain! {
5454
if let hir::ExprKind::MethodCall(ref method, _, ref args, _) = e.kind;
5555
if args.len() == 2;
56-
if method.ident.as_str() == "map";
56+
if method.ident.name == sym::map;
5757
let ty = cx.typeck_results().expr_ty(&args[0]);
5858
if is_type_diagnostic_item(cx, ty, sym::option_type) || match_trait_method(cx, e, &paths::ITERATOR);
5959
if let hir::ExprKind::Closure(_, _, body_id, _, _) = args[1].kind;

clippy_lints/src/map_identity.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl<'tcx> LateLintPass<'tcx> for MapIdentity {
6363
fn get_map_argument<'a>(cx: &LateContext<'_>, expr: &'a Expr<'a>) -> Option<&'a [Expr<'a>]> {
6464
if_chain! {
6565
if let ExprKind::MethodCall(ref method, _, ref args, _) = expr.kind;
66-
if args.len() == 2 && method.ident.as_str() == "map";
66+
if args.len() == 2 && method.ident.name == sym::map;
6767
let caller_ty = cx.typeck_results().expr_ty(&args[0]);
6868
if match_trait_method(cx, expr, &paths::ITERATOR)
6969
|| is_type_diagnostic_item(cx, caller_ty, sym::result_type)

clippy_lints/src/methods/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3095,7 +3095,7 @@ fn lint_flat_map_identity<'tcx>(
30953095
if let hir::ExprKind::Path(hir::QPath::Resolved(_, ref path)) = body.value.kind;
30963096

30973097
if path.segments.len() == 1;
3098-
if path.segments[0].ident.as_str() == binding_ident.as_str();
3098+
if path.segments[0].ident.name == binding_ident.name;
30993099

31003100
then {
31013101
apply_lint("called `flat_map(|x| x)` on an `Iterator`");

clippy_lints/src/minmax.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ fn min_max<'a>(cx: &LateContext<'_>, expr: &'a Expr<'a>) -> Option<(MinMax, Cons
8989
if let [obj, _] = args;
9090
if cx.typeck_results().expr_ty(obj).is_floating_point() || match_trait_method(cx, expr, &paths::ORD);
9191
then {
92-
if path.ident.as_str() == sym!(max).as_str() {
92+
if path.ident.name == sym!(max) {
9393
fetch_const(cx, args, MinMax::Max)
94-
} else if path.ident.as_str() == sym!(min).as_str() {
94+
} else if path.ident.name == sym!(min) {
9595
fetch_const(cx, args, MinMax::Min)
9696
} else {
9797
None

clippy_lints/src/missing_doc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl MissingDoc {
6363
if let Some(meta) = list.get(0);
6464
if let Some(name) = meta.ident();
6565
then {
66-
name.as_str() == "include"
66+
name.name == sym::include
6767
} else {
6868
false
6969
}

clippy_lints/src/needless_pass_by_value.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use rustc_infer::infer::TyCtxtInferExt;
1313
use rustc_lint::{LateContext, LateLintPass};
1414
use rustc_middle::ty::{self, TypeFoldable};
1515
use rustc_session::{declare_lint_pass, declare_tool_lint};
16+
use rustc_span::symbol::kw;
1617
use rustc_span::{sym, Span};
1718
use rustc_target::spec::abi::Abi;
1819
use rustc_trait_selection::traits;
@@ -153,7 +154,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
153154
// Ignore `self`s.
154155
if idx == 0 {
155156
if let PatKind::Binding(.., ident, _) = arg.pat.kind {
156-
if ident.as_str() == "self" {
157+
if ident.name == kw::SelfLower {
157158
continue;
158159
}
159160
}

clippy_lints/src/option_if_let_else.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ declare_lint_pass!(OptionIfLetElse => [OPTION_IF_LET_ELSE]);
6666
/// Returns true iff the given expression is the result of calling `Result::ok`
6767
fn is_result_ok(cx: &LateContext<'_>, expr: &'_ Expr<'_>) -> bool {
6868
if let ExprKind::MethodCall(ref path, _, &[ref receiver], _) = &expr.kind {
69-
path.ident.name.to_ident_string() == "ok"
69+
path.ident.name.as_str() == "ok"
7070
&& is_type_diagnostic_item(cx, &cx.typeck_results().expr_ty(&receiver), sym::result_type)
7171
} else {
7272
false

clippy_lints/src/shadow.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,5 +389,5 @@ fn is_self_shadow(name: Symbol, expr: &Expr<'_>) -> bool {
389389
}
390390

391391
fn path_eq_name(name: Symbol, path: &Path<'_>) -> bool {
392-
!path.is_global() && path.segments.len() == 1 && path.segments[0].ident.as_str() == name.as_str()
392+
!path.is_global() && path.segments.len() == 1 && path.segments[0].ident.name == name
393393
}

clippy_lints/src/swap.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ fn check_manual_swap(cx: &LateContext<'_>, block: &Block<'_>) {
9191
if let ExprKind::Path(QPath::Resolved(None, ref rhs2)) = rhs2.kind;
9292
if rhs2.segments.len() == 1;
9393

94-
if ident.as_str() == rhs2.segments[0].ident.as_str();
94+
if ident.name == rhs2.segments[0].ident.name;
9595
if eq_expr_value(cx, tmp_init, lhs1);
9696
if eq_expr_value(cx, rhs1, lhs2);
9797
then {

clippy_lints/src/unnecessary_sort_by.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ fn detect_lint(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<LintTrigger> {
183183
Param { pat: Pat { kind: PatKind::Binding(_, _, right_ident, _), .. }, .. }
184184
] = &closure_body.params;
185185
if let ExprKind::MethodCall(method_path, _, [ref left_expr, ref right_expr], _) = &closure_body.value.kind;
186-
if method_path.ident.name.to_ident_string() == "cmp";
186+
if method_path.ident.name == sym::cmp;
187187
then {
188188
let (closure_body, closure_arg, reverse) = if mirrored_exprs(
189189
&cx,

clippy_lints/src/useless_conversion.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ impl<'tcx> LateLintPass<'tcx> for UselessConversion {
8080
);
8181
}
8282
}
83-
if match_trait_method(cx, e, &paths::INTO_ITERATOR) && &*name.ident.as_str() == "into_iter" {
83+
if match_trait_method(cx, e, &paths::INTO_ITERATOR) && name.ident.name == sym::into_iter {
8484
if let Some(parent_expr) = get_parent_expr(cx, e) {
8585
if let ExprKind::MethodCall(ref parent_name, ..) = parent_expr.kind {
86-
if &*parent_name.ident.as_str() != "into_iter" {
86+
if parent_name.ident.name != sym::into_iter {
8787
return;
8888
}
8989
}

clippy_lints/src/utils/attrs.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use rustc_ast::ast;
22
use rustc_errors::Applicability;
33
use rustc_session::Session;
4+
use rustc_span::sym;
45
use std::str::FromStr;
56

67
/// Deprecation status of attributes known by Clippy.
@@ -64,11 +65,11 @@ pub fn get_attr<'a>(
6465
return false;
6566
};
6667
let attr_segments = &attr.path.segments;
67-
if attr_segments.len() == 2 && attr_segments[0].ident.to_string() == "clippy" {
68+
if attr_segments.len() == 2 && attr_segments[0].ident.name == sym::clippy {
6869
BUILTIN_ATTRIBUTES
6970
.iter()
70-
.find_map(|(builtin_name, deprecation_status)| {
71-
if *builtin_name == attr_segments[1].ident.to_string() {
71+
.find_map(|&(builtin_name, ref deprecation_status)| {
72+
if attr_segments[1].ident.name.as_str() == builtin_name {
7273
Some(deprecation_status)
7374
} else {
7475
None
@@ -99,7 +100,7 @@ pub fn get_attr<'a>(
99100
},
100101
DeprecationStatus::None => {
101102
diag.cancel();
102-
attr_segments[1].ident.to_string() == name
103+
attr_segments[1].ident.name.as_str() == name
103104
},
104105
}
105106
},

clippy_lints/src/utils/hir_utils.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl<'a, 'tcx> SpanlessEq<'a, 'tcx> {
8686
lb == rb && l_mut == r_mut && self.eq_expr(le, re)
8787
},
8888
(&ExprKind::Continue(li), &ExprKind::Continue(ri)) => {
89-
both(&li.label, &ri.label, |l, r| l.ident.as_str() == r.ident.as_str())
89+
both(&li.label, &ri.label, |l, r| l.ident.name == r.ident.name)
9090
},
9191
(&ExprKind::Assign(ref ll, ref lr, _), &ExprKind::Assign(ref rl, ref rr, _)) => {
9292
self.allow_side_effects && self.eq_expr(ll, rl) && self.eq_expr(lr, rr)
@@ -102,7 +102,7 @@ impl<'a, 'tcx> SpanlessEq<'a, 'tcx> {
102102
})
103103
},
104104
(&ExprKind::Break(li, ref le), &ExprKind::Break(ri, ref re)) => {
105-
both(&li.label, &ri.label, |l, r| l.ident.as_str() == r.ident.as_str())
105+
both(&li.label, &ri.label, |l, r| l.ident.name == r.ident.name)
106106
&& both(le, re, |l, r| self.eq_expr(l, r))
107107
},
108108
(&ExprKind::Box(ref l), &ExprKind::Box(ref r)) => self.eq_expr(l, r),
@@ -121,7 +121,7 @@ impl<'a, 'tcx> SpanlessEq<'a, 'tcx> {
121121
},
122122
(&ExprKind::Lit(ref l), &ExprKind::Lit(ref r)) => l.node == r.node,
123123
(&ExprKind::Loop(ref lb, ref ll, ref lls), &ExprKind::Loop(ref rb, ref rl, ref rls)) => {
124-
lls == rls && self.eq_block(lb, rb) && both(ll, rl, |l, r| l.ident.as_str() == r.ident.as_str())
124+
lls == rls && self.eq_block(lb, rb) && both(ll, rl, |l, r| l.ident.name == r.ident.name)
125125
},
126126
(&ExprKind::Match(ref le, ref la, ref ls), &ExprKind::Match(ref re, ref ra, ref rs)) => {
127127
ls == rs
@@ -188,7 +188,7 @@ impl<'a, 'tcx> SpanlessEq<'a, 'tcx> {
188188

189189
pub fn eq_fieldpat(&mut self, left: &FieldPat<'_>, right: &FieldPat<'_>) -> bool {
190190
let (FieldPat { ident: li, pat: lp, .. }, FieldPat { ident: ri, pat: rp, .. }) = (&left, &right);
191-
li.name.as_str() == ri.name.as_str() && self.eq_pat(lp, rp)
191+
li.name == ri.name && self.eq_pat(lp, rp)
192192
}
193193

194194
/// Checks whether two patterns are the same.
@@ -202,7 +202,7 @@ impl<'a, 'tcx> SpanlessEq<'a, 'tcx> {
202202
self.eq_qpath(lp, rp) && over(la, ra, |l, r| self.eq_pat(l, r)) && ls == rs
203203
},
204204
(&PatKind::Binding(ref lb, .., ref li, ref lp), &PatKind::Binding(ref rb, .., ref ri, ref rp)) => {
205-
lb == rb && li.name.as_str() == ri.name.as_str() && both(lp, rp, |l, r| self.eq_pat(l, r))
205+
lb == rb && li.name == ri.name && both(lp, rp, |l, r| self.eq_pat(l, r))
206206
},
207207
(&PatKind::Path(ref l), &PatKind::Path(ref r)) => self.eq_qpath(l, r),
208208
(&PatKind::Lit(ref l), &PatKind::Lit(ref r)) => self.eq_expr(l, r),
@@ -263,8 +263,7 @@ impl<'a, 'tcx> SpanlessEq<'a, 'tcx> {
263263
pub fn eq_path_segment(&mut self, left: &PathSegment<'_>, right: &PathSegment<'_>) -> bool {
264264
// The == of idents doesn't work with different contexts,
265265
// we have to be explicit about hygiene
266-
left.ident.as_str() == right.ident.as_str()
267-
&& both(&left.args, &right.args, |l, r| self.eq_path_parameters(l, r))
266+
left.ident.name == right.ident.name && both(&left.args, &right.args, |l, r| self.eq_path_parameters(l, r))
268267
}
269268

270269
pub fn eq_ty(&mut self, left: &Ty<'_>, right: &Ty<'_>) -> bool {

0 commit comments

Comments
 (0)