Skip to content

Commit 85ef017

Browse files
committed
book: encourage the use of clippy_utils::sym
Also, explain how new symbols can be added to this module in order to compare names.
1 parent ebbbbeb commit 85ef017

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

book/src/development/common_tools_writing_lints.md

Lines changed: 3 additions & 4 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.as_str() == "some_method"
71+
&& path.ident.name == sym::some_method
7272
// Optionally, check the type of the self argument.
7373
// - See "Checking for a specific type"
7474
{
@@ -85,9 +85,8 @@ to check for. All of these methods only check for the base type, generic
8585
arguments have to be checked separately.
8686

8787
```rust
88-
use clippy_utils::paths;
88+
use clippy_utils::{paths, sym};
8989
use clippy_utils::res::MaybeDef;
90-
use rustc_span::symbol::sym;
9190
use rustc_hir::LangItem;
9291

9392
impl LateLintPass<'_> for MyStructLint {
@@ -123,8 +122,8 @@ There are three ways to do this, depending on if the target trait has a
123122
diagnostic item, lang item or neither.
124123

125124
```rust
125+
use clippy_utils::sym;
126126
use clippy_utils::ty::implements_trait;
127-
use rustc_span::symbol::sym;
128127

129128
impl LateLintPass<'_> for MyStructLint {
130129
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {

book/src/development/method_checking.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ the [`ExprKind`] that we can access from `expr.kind`:
1515
```rust
1616
use rustc_hir as hir;
1717
use rustc_lint::{LateContext, LateLintPass};
18-
use rustc_span::sym;
1918
use clippy_utils::res::{MaybeDef, MaybeTypeckRes};
19+
use clippy_utils::sym;
2020

2121
impl<'tcx> LateLintPass<'tcx> for OurFancyMethodLint {
2222
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
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.as_str() == "our_fancy_method"
26+
&& path.ident.name == sym::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,6 +41,10 @@ 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+
New symbols such as `our_fancy_method` need to be added to the `clippy_utils::sym` module.
45+
This module extends the list of symbols already provided by the compiler crates
46+
in `rustc_span::sym`.
47+
4448
## Checking if a `impl` block implements a method
4549

4650
While sometimes we want to check whether a method is being called or not, other
@@ -56,11 +60,10 @@ Let us take a look at how we might check for the implementation of
5660
`our_fancy_method` on a type:
5761

5862
```rust
63+
use clippy_utils::{return_ty, sym};
5964
use clippy_utils::res::MaybeDef;
60-
use clippy_utils::return_ty;
6165
use rustc_hir::{ImplItem, ImplItemKind};
6266
use rustc_lint::{LateContext, LateLintPass};
63-
use rustc_span::symbol::sym;
6467

6568
impl<'tcx> LateLintPass<'tcx> for MyTypeImpl {
6669
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx ImplItem<'_>) {

book/src/development/trait_checking.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ providing the `LateContext` (`cx`), our expression at hand, and
1717
the symbol of the trait in question:
1818

1919
```rust
20+
use clippy_utils::sym;
2021
use clippy_utils::ty::implements_trait;
2122
use rustc_hir::Expr;
2223
use rustc_lint::{LateContext, LateLintPass};
23-
use rustc_span::symbol::sym;
2424

2525
impl LateLintPass<'_> for CheckIteratorTraitLint {
2626
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
@@ -124,8 +124,8 @@ The following code demonstrates how to do this:
124124
```rust
125125

126126
use rustc_middle::ty::Ty;
127+
use clippy_utils::sym;
127128
use clippy_utils::ty::implements_trait;
128-
use rustc_span::symbol::sym;
129129

130130
let ty = todo!("Get the `Foo` type to check for a trait implementation");
131131
let borrow_id = cx.tcx.get_diagnostic_item(sym::Borrow).unwrap(); // avoid unwrap in real code

0 commit comments

Comments
 (0)