Skip to content

Commit 94387ef

Browse files
committed
Fixes based on reviews
1 parent 3c50a0a commit 94387ef

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

book/src/development/trait_checking.md

+14-18
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ that we want to examine has a [diagnostic item][diagnostic_items],
1111
As explained in the [Rust Compiler Development Guide][rustc_dev_guide], diagnostic items
1212
are introduced for identifying types via [Symbols][symbol].
1313

14-
While the Rust Compiler Development Guide has [a section][using_diagnostic_items] on
15-
how to check for a specific trait on a type `Ty`, Clippy provides
16-
a helper function `is_trait_method`, which simplifies the process for us.
17-
1814
For instance, if we want to examine whether an expression implements
1915
the `Iterator` trait, we could simply write the following code,
2016
providing the `LateContext` (`cx`), our expression at hand, and
@@ -28,9 +24,13 @@ use rustc_span::symbol::sym;
2824

2925
impl LateLintPass<'_> for CheckIteratorTraitLint {
3026
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
31-
if is_trait_method(cx, expr, sym::Iterator) {
32-
println!("This expression implements `Iterator` trait!");
33-
}
27+
let implements_iterator = cx.tcx.get_diagnostic_item(sym::Iterator).map_or(false, |id| {
28+
implements_trait(cx, cx.typeck_results().expr_ty(arg), id, &[])
29+
});
30+
if implements_iterator {
31+
// [...]
32+
}
33+
3434
}
3535
}
3636
```
@@ -40,9 +40,8 @@ impl LateLintPass<'_> for CheckIteratorTraitLint {
4040
## Using Lang Items
4141

4242
Besides diagnostic items, we can also use [`lang_items`][lang_items].
43-
Take a look at the documentation and we find that `LanguageItems` contains
44-
all language items both from the current crate or its
45-
dependencies.
43+
Take a look at the documentation to find that `LanguageItems` contains
44+
all language items defined in the compiler.
4645

4746
Using one of its `*_trait` method, we could obtain the [DefId] of any
4847
specific item, such as `Clone`, `Copy`, `Drop`, `Eq`, which are familiar
@@ -73,11 +72,11 @@ impl LateLintPass<'_> for CheckDropTraitLint {
7372

7473
## Using Type Path
7574

76-
If neither diagnostic item or lang item is available, we can use
75+
If neither diagnostic item nor a language item is available, we can use
7776
[`clippy_utils::paths`][paths] with the `match_trait_method` to determine trait
7877
implementation.
7978

80-
> **Note**: This approach should be avoided if possible.
79+
> **Note**: This approach should be avoided if possible, the best thing to do would be to make a PR to [`rust-lang/rust`][rust].
8180
8281
Below, we check if the given `expr` implements `tokio`'s
8382
[`AsyncReadExt`][AsyncReadExt] trait:
@@ -89,17 +88,13 @@ use rustc_lint::{LateContext, LateLintPass};
8988

9089
impl LateLintPass<'_> for CheckTokioAsyncReadExtTrait {
9190
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
92-
if match_trait_method(cx, expr, &paths::TOKIO_IO_ASYNCREADEXT) {
93-
println!("`expr` implements `TOKIO_IO_ASYNCREADEXT` trait!");
91+
if match_trait_method(cx, expr, &paths::CORE_ITER_CLONED) {
92+
println!("`expr` implements `CORE_ITER_CLONED` trait!");
9493
}
9594
}
9695
}
9796
```
9897

99-
> **Note**: Even though all the `clippy_utils` methods we have seen in this
100-
> chapter takes `expr` as a parameter, these methods are actually using
101-
> each expression's `HirId` under the hood.
102-
10398
[AsyncReadExt]: https://docs.rs/tokio/latest/tokio/io/trait.AsyncReadExt.html
10499
[DefId]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/def_id/struct.DefId.html
105100
[diagnostic_items]: https://rustc-dev-guide.rust-lang.org/diagnostics/diagnostic-items.html
@@ -110,3 +105,4 @@ impl LateLintPass<'_> for CheckTokioAsyncReadExtTrait {
110105
[symbol_index]: https://doc.rust-lang.org/beta/nightly-rustc/rustc_span/symbol/sym/index.html
111106
[TyCtxt]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/context/struct.TyCtxt.html
112107
[using_diagnostic_items]: https://rustc-dev-guide.rust-lang.org/diagnostics/diagnostic-items.html#using-diagnostic-items
108+
[rust]: https://github.com/rust-lang/rust

0 commit comments

Comments
 (0)