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
}

0 commit comments

Comments
 (0)