Skip to content

Commit f2f0175

Browse files
committed
Auto merge of #13543 - GnomedDev:symbol-comparisons, r=y21
Add internal lint to check for slow symbol comparisons See the conversation on [Zulip](https://rust-lang.zulipchat.com/#narrow/stream/257328-clippy/topic/Checking.20a.20Symbol.20is.20equal.20to.20a.20string.20literal). changelog: none
2 parents 6a79588 + 979e297 commit f2f0175

36 files changed

+200
-63
lines changed

book/src/development/common_tools_writing_lints.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl<'tcx> LateLintPass<'tcx> for MyStructLint {
6868
// Check our expr is calling a method
6969
if let hir::ExprKind::MethodCall(path, _, _self_arg, ..) = &expr.kind
7070
// Check the name of this method is `some_method`
71-
&& path.ident.name == sym!(some_method)
71+
&& path.ident.name.as_str() == "some_method"
7272
// Optionally, check the type of the self argument.
7373
// - See "Checking for a specific type"
7474
{
@@ -167,7 +167,7 @@ impl<'tcx> LateLintPass<'tcx> for MyTypeImpl {
167167
// Check if item is a method/function
168168
if let ImplItemKind::Fn(ref signature, _) = impl_item.kind
169169
// Check the method is named `some_method`
170-
&& impl_item.ident.name == sym!(some_method)
170+
&& impl_item.ident.name.as_str() == "some_method"
171171
// We can also check it has a parameter `self`
172172
&& signature.decl.implicit_self.has_implicit_self()
173173
// We can go further and even check if its return type is `String`

book/src/development/method_checking.md

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
In some scenarios we might want to check for methods when developing
44
a lint. There are two kinds of questions that we might be curious about:
55

6-
- Invocation: Does an expression call a specific method?
7-
- Definition: Does an `impl` define a method?
6+
- Invocation: Does an expression call a specific method?
7+
- Definition: Does an `impl` define a method?
88

99
## Checking if an `expr` is calling a specific method
1010

@@ -23,7 +23,7 @@ impl<'tcx> LateLintPass<'tcx> for OurFancyMethodLint {
2323
// Check our expr is calling a method with pattern matching
2424
if let hir::ExprKind::MethodCall(path, _, [self_arg, ..]) = &expr.kind
2525
// Check if the name of this method is `our_fancy_method`
26-
&& path.ident.name == sym!(our_fancy_method)
26+
&& path.ident.name.as_str() == "our_fancy_method"
2727
// We can check the type of the self argument whenever necessary.
2828
// (It's necessary if we want to check that method is specifically belonging to a specific trait,
2929
// for example, a `map` method could belong to user-defined trait instead of to `Iterator`)
@@ -41,10 +41,6 @@ information on the pattern matching. As mentioned in [Define
4141
Lints](defining_lints.md#lint-types), the `methods` lint type is full of pattern
4242
matching with `MethodCall` in case the reader wishes to explore more.
4343

44-
Additionally, we use the [`clippy_utils::sym!`][sym] macro to conveniently
45-
convert an input `our_fancy_method` into a `Symbol` and compare that symbol to
46-
the [`Ident`]'s name in the [`PathSegment`] in the [`MethodCall`].
47-
4844
## Checking if a `impl` block implements a method
4945

5046
While sometimes we want to check whether a method is being called or not, other
@@ -71,7 +67,7 @@ impl<'tcx> LateLintPass<'tcx> for MyTypeImpl {
7167
// Check if item is a method/function
7268
if let ImplItemKind::Fn(ref signature, _) = impl_item.kind
7369
// Check the method is named `our_fancy_method`
74-
&& impl_item.ident.name == sym!(our_fancy_method)
70+
&& impl_item.ident.name.as_str() == "our_fancy_method"
7571
// We can also check it has a parameter `self`
7672
&& signature.decl.implicit_self.has_implicit_self()
7773
// We can go even further and even check if its return type is `String`
@@ -85,9 +81,6 @@ impl<'tcx> LateLintPass<'tcx> for MyTypeImpl {
8581

8682
[`check_impl_item`]: https://doc.rust-lang.org/stable/nightly-rustc/rustc_lint/trait.LateLintPass.html#method.check_impl_item
8783
[`ExprKind`]: https://doc.rust-lang.org/beta/nightly-rustc/rustc_hir/hir/enum.ExprKind.html
88-
[`Ident`]: https://doc.rust-lang.org/beta/nightly-rustc/rustc_span/symbol/struct.Ident.html
8984
[`ImplItem`]: https://doc.rust-lang.org/stable/nightly-rustc/rustc_hir/hir/struct.ImplItem.html
9085
[`LateLintPass`]: https://doc.rust-lang.org/stable/nightly-rustc/rustc_lint/trait.LateLintPass.html
9186
[`MethodCall`]: https://doc.rust-lang.org/beta/nightly-rustc/rustc_hir/hir/enum.ExprKind.html#variant.MethodCall
92-
[`PathSegment`]: https://doc.rust-lang.org/beta/nightly-rustc/rustc_hir/hir/struct.PathSegment.html
93-
[sym]: https://doc.rust-lang.org/stable/nightly-rustc/clippy_utils/macro.sym.html

clippy_lints/src/casts/cast_ptr_alignment.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>) {
2020
);
2121
lint_cast_ptr_alignment(cx, expr, cast_from, cast_to);
2222
} else if let ExprKind::MethodCall(method_path, self_arg, [], _) = &expr.kind {
23-
if method_path.ident.name == sym!(cast)
23+
if method_path.ident.name.as_str() == "cast"
2424
&& let Some(generic_args) = method_path.args
2525
&& let [GenericArg::Type(cast_to)] = generic_args.args
2626
// There probably is no obvious reason to do this, just to be consistent with `as` cases.

clippy_lints/src/declared_lints.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ pub static LINTS: &[&crate::LintInfo] = &[
2828
#[cfg(feature = "internal")]
2929
crate::utils::internal_lints::produce_ice::PRODUCE_ICE_INFO,
3030
#[cfg(feature = "internal")]
31+
crate::utils::internal_lints::slow_symbol_comparisons::SLOW_SYMBOL_COMPARISONS_INFO,
32+
#[cfg(feature = "internal")]
3133
crate::utils::internal_lints::unnecessary_def_path::UNNECESSARY_DEF_PATH_INFO,
3234
#[cfg(feature = "internal")]
3335
crate::utils::internal_lints::unsorted_clippy_utils_paths::UNSORTED_CLIPPY_UTILS_PATHS_INFO,

clippy_lints/src/explicit_write.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitWrite {
5858
// match call to write_fmt
5959
&& let ExprKind::MethodCall(write_fun, write_recv, [write_arg], _) = *look_in_block(cx, &write_call.kind)
6060
&& let ExprKind::Call(write_recv_path, []) = write_recv.kind
61-
&& write_fun.ident.name == sym!(write_fmt)
61+
&& write_fun.ident.name.as_str() == "write_fmt"
6262
&& let Some(def_id) = path_def_id(cx, write_recv_path)
6363
{
6464
// match calls to std::io::stdout() / std::io::stderr ()

clippy_lints/src/from_raw_with_void_ptr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl LateLintPass<'_> for FromRawWithVoidPtr {
4141
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
4242
if let ExprKind::Call(box_from_raw, [arg]) = expr.kind
4343
&& let ExprKind::Path(QPath::TypeRelative(ty, seg)) = box_from_raw.kind
44-
&& seg.ident.name == sym!(from_raw)
44+
&& seg.ident.name.as_str() == "from_raw"
4545
&& let Some(type_str) = path_def_id(cx, ty).and_then(|id| def_id_matches_type(cx, id))
4646
&& let arg_kind = cx.typeck_results().expr_ty(arg).kind()
4747
&& let ty::RawPtr(ty, _) = arg_kind

clippy_lints/src/implicit_hasher.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ impl<'tcx> Visitor<'tcx> for ImplicitHasherConstructorVisitor<'_, '_, 'tcx> {
340340
if self.cx.tcx.is_diagnostic_item(sym::HashMap, ty_did) {
341341
if method.ident.name == sym::new {
342342
self.suggestions.insert(e.span, "HashMap::default()".to_string());
343-
} else if method.ident.name == sym!(with_capacity) {
343+
} else if method.ident.name.as_str() == "with_capacity" {
344344
self.suggestions.insert(
345345
e.span,
346346
format!(
@@ -352,7 +352,7 @@ impl<'tcx> Visitor<'tcx> for ImplicitHasherConstructorVisitor<'_, '_, 'tcx> {
352352
} else if self.cx.tcx.is_diagnostic_item(sym::HashSet, ty_did) {
353353
if method.ident.name == sym::new {
354354
self.suggestions.insert(e.span, "HashSet::default()".to_string());
355-
} else if method.ident.name == sym!(with_capacity) {
355+
} else if method.ident.name.as_str() == "with_capacity" {
356356
self.suggestions.insert(
357357
e.span,
358358
format!(

clippy_lints/src/infinite_iter.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ fn is_infinite(cx: &LateContext<'_>, expr: &Expr<'_>) -> Finiteness {
157157
.and(cap);
158158
}
159159
}
160-
if method.ident.name == sym!(flat_map) && args.len() == 1 {
160+
if method.ident.name.as_str() == "flat_map" && args.len() == 1 {
161161
if let ExprKind::Closure(&Closure { body, .. }) = args[0].kind {
162162
let body = cx.tcx.hir().body(body);
163163
return is_infinite(cx, body.value);
@@ -224,7 +224,7 @@ fn complete_infinite_iter(cx: &LateContext<'_>, expr: &Expr<'_>) -> Finiteness {
224224
return MaybeInfinite.and(is_infinite(cx, receiver));
225225
}
226226
}
227-
if method.ident.name == sym!(last) && args.is_empty() {
227+
if method.ident.name.as_str() == "last" && args.is_empty() {
228228
let not_double_ended = cx
229229
.tcx
230230
.get_diagnostic_item(sym::DoubleEndedIterator)
@@ -234,7 +234,7 @@ fn complete_infinite_iter(cx: &LateContext<'_>, expr: &Expr<'_>) -> Finiteness {
234234
if not_double_ended {
235235
return is_infinite(cx, receiver);
236236
}
237-
} else if method.ident.name == sym!(collect) {
237+
} else if method.ident.name.as_str() == "collect" {
238238
let ty = cx.typeck_results().expr_ty(expr);
239239
if matches!(
240240
get_type_diagnostic_name(cx, ty),

clippy_lints/src/iter_without_into_iter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ impl LateLintPass<'_> for IterWithoutIntoIter {
142142
ty.peel_refs().is_slice() || get_adt_inherent_method(cx, ty, expected_method_name).is_some()
143143
})
144144
&& let Some(iter_assoc_span) = imp.items.iter().find_map(|item| {
145-
if item.ident.name == sym!(IntoIter) {
145+
if item.ident.name.as_str() == "IntoIter" {
146146
Some(cx.tcx.hir().impl_item(item.id).expect_type().span)
147147
} else {
148148
None

clippy_lints/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
603603
store.register_late_pass(|_| {
604604
Box::new(utils::internal_lints::almost_standard_lint_formulation::AlmostStandardFormulation::new())
605605
});
606+
store.register_late_pass(|_| Box::new(utils::internal_lints::slow_symbol_comparisons::SlowSymbolComparisons));
606607
}
607608

608609
store.register_late_pass(move |_| Box::new(operators::arithmetic_side_effects::ArithmeticSideEffects::new(conf)));

0 commit comments

Comments
 (0)