Skip to content

Commit 35f8bff

Browse files
authored
book: cleanups (#15864)
changelog: none
2 parents 1f0b8b8 + 1ffd092 commit 35f8bff

File tree

4 files changed

+17
-14
lines changed

4 files changed

+17
-14
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/macro_expansions.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ before emitting suggestions to the end user to avoid false positives.
3737

3838
Several functions are available for working with macros.
3939

40-
### The `Span.from_expansion` method
40+
### The `Span::from_expansion` method
4141

4242
We could utilize a `span`'s [`from_expansion`] method, which
4343
detects if the `span` is from a macro expansion / desugaring.
@@ -50,7 +50,7 @@ if expr.span.from_expansion() {
5050
}
5151
```
5252

53-
### `Span.ctxt` method
53+
### `Span::ctxt` method
5454

5555
The `span`'s context, given by the method [`ctxt`] and returning [SyntaxContext],
5656
represents if the span is from a macro expansion and, if it is, which

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: 5 additions & 4 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<'_>) {
@@ -53,7 +53,7 @@ For instance, if we want to examine whether an expression `expr` implements
5353
we can check that the `Ty` of the `expr` implements the trait:
5454

5555
```rust
56-
use clippy_utils::implements_trait;
56+
use clippy_utils::ty::implements_trait;
5757
use rustc_hir::Expr;
5858
use rustc_lint::{LateContext, LateLintPass};
5959

@@ -79,7 +79,8 @@ If neither diagnostic item nor a language item is available, we can use
7979
Below, we check if the given `expr` implements [`core::iter::Step`](https://doc.rust-lang.org/std/iter/trait.Step.html):
8080

8181
```rust
82-
use clippy_utils::{implements_trait, paths};
82+
use clippy_utils::paths;
83+
use clippy_utils::ty::implements_trait;
8384
use rustc_hir::Expr;
8485
use rustc_lint::{LateContext, LateLintPass};
8586

@@ -124,8 +125,8 @@ The following code demonstrates how to do this:
124125
```rust
125126

126127
use rustc_middle::ty::Ty;
128+
use clippy_utils::sym;
127129
use clippy_utils::ty::implements_trait;
128-
use rustc_span::symbol::sym;
129130

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

0 commit comments

Comments
 (0)