Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions book/src/development/common_tools_writing_lints.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl<'tcx> LateLintPass<'tcx> for MyStructLint {
// Check our expr is calling a method
if let hir::ExprKind::MethodCall(path, _, _self_arg, ..) = &expr.kind
// Check the name of this method is `some_method`
&& path.ident.name.as_str() == "some_method"
&& path.ident.name == sym::some_method
// Optionally, check the type of the self argument.
// - See "Checking for a specific type"
{
Expand All @@ -85,9 +85,8 @@ to check for. All of these methods only check for the base type, generic
arguments have to be checked separately.

```rust
use clippy_utils::paths;
use clippy_utils::{paths, sym};
use clippy_utils::res::MaybeDef;
use rustc_span::symbol::sym;
use rustc_hir::LangItem;

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

```rust
use clippy_utils::sym;
use clippy_utils::ty::implements_trait;
use rustc_span::symbol::sym;

impl LateLintPass<'_> for MyStructLint {
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
Expand Down
4 changes: 2 additions & 2 deletions book/src/development/macro_expansions.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ before emitting suggestions to the end user to avoid false positives.

Several functions are available for working with macros.

### The `Span.from_expansion` method
### The `Span::from_expansion` method

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

### `Span.ctxt` method
### `Span::ctxt` method

The `span`'s context, given by the method [`ctxt`] and returning [SyntaxContext],
represents if the span is from a macro expansion and, if it is, which
Expand Down
11 changes: 7 additions & 4 deletions book/src/development/method_checking.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ the [`ExprKind`] that we can access from `expr.kind`:
```rust
use rustc_hir as hir;
use rustc_lint::{LateContext, LateLintPass};
use rustc_span::sym;
use clippy_utils::res::{MaybeDef, MaybeTypeckRes};
use clippy_utils::sym;

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

New symbols such as `our_fancy_method` need to be added to the `clippy_utils::sym` module.
This module extends the list of symbols already provided by the compiler crates
in `rustc_span::sym`.

## Checking if a `impl` block implements a method

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

```rust
use clippy_utils::{return_ty, sym};
use clippy_utils::res::MaybeDef;
use clippy_utils::return_ty;
use rustc_hir::{ImplItem, ImplItemKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_span::symbol::sym;

impl<'tcx> LateLintPass<'tcx> for MyTypeImpl {
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx ImplItem<'_>) {
Expand Down
9 changes: 5 additions & 4 deletions book/src/development/trait_checking.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ providing the `LateContext` (`cx`), our expression at hand, and
the symbol of the trait in question:

```rust
use clippy_utils::sym;
use clippy_utils::ty::implements_trait;
use rustc_hir::Expr;
use rustc_lint::{LateContext, LateLintPass};
use rustc_span::symbol::sym;

impl LateLintPass<'_> for CheckIteratorTraitLint {
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
Expand Down Expand Up @@ -53,7 +53,7 @@ For instance, if we want to examine whether an expression `expr` implements
we can check that the `Ty` of the `expr` implements the trait:

```rust
use clippy_utils::implements_trait;
use clippy_utils::ty::implements_trait;
use rustc_hir::Expr;
use rustc_lint::{LateContext, LateLintPass};

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

```rust
use clippy_utils::{implements_trait, paths};
use clippy_utils::paths;
use clippy_utils::ty::implements_trait;
use rustc_hir::Expr;
use rustc_lint::{LateContext, LateLintPass};

Expand Down Expand Up @@ -124,8 +125,8 @@ The following code demonstrates how to do this:
```rust

use rustc_middle::ty::Ty;
use clippy_utils::sym;
use clippy_utils::ty::implements_trait;
use rustc_span::symbol::sym;

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