2020-05-28 08:45:24 -05:00
|
|
|
# Common tools for writing lints
|
|
|
|
|
|
|
|
You may need following tooltips to catch up with common operations.
|
|
|
|
|
|
|
|
- [Common tools for writing lints](#common-tools-for-writing-lints)
|
|
|
|
- [Retrieving the type of an expression](#retrieving-the-type-of-an-expression)
|
2020-06-09 09:36:01 -05:00
|
|
|
- [Checking if an expression is calling a specific method](#checking-if-an-expr-is-calling-a-specific-method)
|
2020-05-28 08:45:24 -05:00
|
|
|
- [Checking if a type implements a specific trait](#checking-if-a-type-implements-a-specific-trait)
|
2021-06-15 10:38:16 -05:00
|
|
|
- [Checking if a type defines a specific method](#checking-if-a-type-defines-a-specific-method)
|
2020-05-28 08:45:24 -05:00
|
|
|
- [Dealing with macros](#dealing-with-macros)
|
|
|
|
|
|
|
|
Useful Rustc dev guide links:
|
|
|
|
- [Stages of compilation](https://rustc-dev-guide.rust-lang.org/compiler-src.html#the-main-stages-of-compilation)
|
2021-09-01 14:55:08 -05:00
|
|
|
- [Diagnostic items](https://rustc-dev-guide.rust-lang.org/diagnostics/diagnostic-items.html)
|
2020-05-28 08:45:24 -05:00
|
|
|
- [Type checking](https://rustc-dev-guide.rust-lang.org/type-checking.html)
|
|
|
|
- [Ty module](https://rustc-dev-guide.rust-lang.org/ty.html)
|
|
|
|
|
|
|
|
# Retrieving the type of an expression
|
|
|
|
|
|
|
|
Sometimes you may want to retrieve the type `Ty` of an expression `Expr`, for example to answer following questions:
|
|
|
|
|
|
|
|
- which type does this expression correspond to (using its [`TyKind`][TyKind])?
|
2020-06-25 18:56:23 -05:00
|
|
|
- is it a sized type?
|
2020-05-28 08:45:24 -05:00
|
|
|
- is it a primitive type?
|
|
|
|
- does it implement a trait?
|
|
|
|
|
2020-07-17 03:47:04 -05:00
|
|
|
This operation is performed using the [`expr_ty()`][expr_ty] method from the [`TypeckResults`][TypeckResults] struct,
|
2020-05-28 08:45:24 -05:00
|
|
|
that gives you access to the underlying structure [`TyS`][TyS].
|
|
|
|
|
|
|
|
Example of use:
|
|
|
|
```rust
|
2020-06-25 15:41:36 -05:00
|
|
|
impl LateLintPass<'_> for MyStructLint {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
|
2020-05-28 08:45:24 -05:00
|
|
|
// Get type of `expr`
|
2020-07-17 03:47:04 -05:00
|
|
|
let ty = cx.typeck_results().expr_ty(expr);
|
2020-05-28 08:45:24 -05:00
|
|
|
// Match its kind to enter its type
|
|
|
|
match ty.kind {
|
|
|
|
ty::Adt(adt_def, _) if adt_def.is_struct() => println!("Our `expr` is a struct!"),
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2020-07-17 03:47:04 -05:00
|
|
|
Similarly in [`TypeckResults`][TypeckResults] methods, you have the [`pat_ty()`][pat_ty] method
|
2020-05-28 08:45:24 -05:00
|
|
|
to retrieve a type from a pattern.
|
|
|
|
|
|
|
|
Two noticeable items here:
|
2020-10-07 04:59:20 -05:00
|
|
|
- `cx` is the lint context [`LateContext`][LateContext]. The two most useful
|
|
|
|
data structures in this context are `tcx` and the `TypeckResults` returned by
|
2020-10-07 10:10:31 -05:00
|
|
|
`LateContext::typeck_results`, allowing us to jump to type definitions and
|
2020-10-07 04:59:20 -05:00
|
|
|
other compilation stages such as HIR.
|
|
|
|
- `typeck_results`'s return value is [`TypeckResults`][TypeckResults] and is
|
|
|
|
created by type checking step, it includes useful information such as types
|
|
|
|
of expressions, ways to resolve methods and so on.
|
2020-05-28 08:45:24 -05:00
|
|
|
|
2020-06-09 09:36:01 -05:00
|
|
|
# Checking if an expr is calling a specific method
|
|
|
|
|
|
|
|
Starting with an `expr`, you can check whether it is calling a specific method `some_method`:
|
|
|
|
|
|
|
|
```rust
|
2020-06-25 15:41:36 -05:00
|
|
|
impl LateLintPass<'_> for MyStructLint {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
|
2020-06-09 09:36:01 -05:00
|
|
|
if_chain! {
|
|
|
|
// Check our expr is calling a method
|
2020-09-06 16:57:24 -05:00
|
|
|
if let hir::ExprKind::MethodCall(path, _, _args, _) = &expr.kind;
|
2020-06-09 09:36:01 -05:00
|
|
|
// Check the name of this method is `some_method`
|
|
|
|
if path.ident.name == sym!(some_method);
|
|
|
|
then {
|
|
|
|
// ...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2020-05-28 08:45:24 -05:00
|
|
|
# Checking if a type implements a specific trait
|
|
|
|
|
2021-08-17 23:40:59 -05:00
|
|
|
There are three ways to do this, depending on if the target trait has a diagnostic item, lang item or neither.
|
2020-05-28 08:45:24 -05:00
|
|
|
|
|
|
|
```rust
|
2021-08-26 05:01:41 -05:00
|
|
|
use clippy_utils::{implements_trait, is_trait_method, match_trait_method, paths};
|
2021-08-17 23:40:59 -05:00
|
|
|
use rustc_span::symbol::sym;
|
2020-05-28 08:45:24 -05:00
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
impl LateLintPass<'_> for MyStructLint {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
|
2021-08-17 23:40:59 -05:00
|
|
|
// 1. Using diagnostic items with the expression
|
|
|
|
// we use `is_trait_method` function from Clippy's utils
|
|
|
|
if is_trait_method(cx, expr, sym::Iterator) {
|
|
|
|
// method call in `expr` belongs to `Iterator` trait
|
2020-05-28 08:45:24 -05:00
|
|
|
}
|
|
|
|
|
2021-08-17 23:40:59 -05:00
|
|
|
// 2. Using lang items with the expression type
|
2020-07-17 03:47:04 -05:00
|
|
|
let ty = cx.typeck_results().expr_ty(expr);
|
2020-05-28 08:45:24 -05:00
|
|
|
if cx.tcx.lang_items()
|
|
|
|
// we are looking for the `DefId` of `Drop` trait in lang items
|
|
|
|
.drop_trait()
|
|
|
|
// then we use it with our type `ty` by calling `implements_trait` from Clippy's utils
|
|
|
|
.map_or(false, |id| implements_trait(cx, ty, id, &[])) {
|
|
|
|
// `expr` implements `Drop` trait
|
|
|
|
}
|
2021-08-17 23:40:59 -05:00
|
|
|
|
|
|
|
// 3. Using the type path with the expression
|
|
|
|
// we use `match_trait_method` function from Clippy's utils
|
|
|
|
if match_trait_method(cx, expr, &paths::INTO) {
|
|
|
|
// `expr` implements `Into` trait
|
|
|
|
}
|
2020-05-28 08:45:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-08-17 23:40:59 -05:00
|
|
|
> Prefer using diagnostic and lang items, if the target trait has one.
|
2020-05-28 08:45:24 -05:00
|
|
|
|
|
|
|
We access lang items through the type context `tcx`. `tcx` is of type [`TyCtxt`][TyCtxt] and is defined in the `rustc_middle` crate.
|
2021-08-17 23:40:59 -05:00
|
|
|
A list of defined paths for Clippy can be found in [paths.rs][paths]
|
2020-05-28 08:45:24 -05:00
|
|
|
|
2020-06-09 09:36:01 -05:00
|
|
|
# Checking if a type defines a specific method
|
|
|
|
|
|
|
|
To check if our type defines a method called `some_method`:
|
|
|
|
|
|
|
|
```rust
|
2021-02-18 05:08:46 -06:00
|
|
|
use clippy_utils::{is_type_diagnostic_item, return_ty};
|
2020-06-09 09:36:01 -05:00
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for MyTypeImpl {
|
|
|
|
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx ImplItem<'_>) {
|
2020-06-09 09:36:01 -05:00
|
|
|
if_chain! {
|
|
|
|
// Check if item is a method/function
|
|
|
|
if let ImplItemKind::Fn(ref signature, _) = impl_item.kind;
|
|
|
|
// Check the method is named `some_method`
|
|
|
|
if impl_item.ident.name == sym!(some_method);
|
|
|
|
// We can also check it has a parameter `self`
|
|
|
|
if signature.decl.implicit_self.has_implicit_self();
|
|
|
|
// We can go further and even check if its return type is `String`
|
|
|
|
if is_type_diagnostic_item(cx, return_ty(cx, impl_item.hir_id), sym!(string_type));
|
|
|
|
then {
|
|
|
|
// ...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2020-05-28 08:45:24 -05:00
|
|
|
# Dealing with macros
|
|
|
|
|
2021-02-18 05:08:46 -06:00
|
|
|
There are several helpers in [`clippy_utils`][utils] to deal with macros:
|
2020-05-28 08:45:24 -05:00
|
|
|
|
|
|
|
- `in_macro()`: detect if the given span is expanded by a macro
|
|
|
|
|
|
|
|
You may want to use this for example to not start linting in any macro.
|
|
|
|
|
|
|
|
```rust
|
|
|
|
macro_rules! foo {
|
|
|
|
($param:expr) => {
|
|
|
|
match $param {
|
|
|
|
"bar" => println!("whatever"),
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
foo!("bar");
|
|
|
|
|
|
|
|
// if we lint the `match` of `foo` call and test its span
|
|
|
|
assert_eq!(in_macro(match_span), true);
|
|
|
|
```
|
|
|
|
|
|
|
|
- `in_external_macro()`: detect if the given span is from an external macro, defined in a foreign crate
|
|
|
|
|
|
|
|
You may want to use it for example to not start linting in macros from other crates
|
|
|
|
|
|
|
|
```rust
|
|
|
|
#[macro_use]
|
|
|
|
extern crate a_crate_with_macros;
|
|
|
|
|
|
|
|
// `foo` is defined in `a_crate_with_macros`
|
|
|
|
foo!("bar");
|
|
|
|
|
|
|
|
// if we lint the `match` of `foo` call and test its span
|
|
|
|
assert_eq!(in_external_macro(cx.sess(), match_span), true);
|
|
|
|
```
|
|
|
|
|
|
|
|
- `differing_macro_contexts()`: returns true if the two given spans are not from the same context
|
|
|
|
|
|
|
|
```rust
|
|
|
|
macro_rules! m {
|
|
|
|
($a:expr, $b:expr) => {
|
|
|
|
if $a.is_some() {
|
|
|
|
$b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let x: Option<u32> = Some(42);
|
|
|
|
m!(x, x.unwrap());
|
|
|
|
|
|
|
|
// These spans are not from the same context
|
|
|
|
// x.is_some() is from inside the macro
|
|
|
|
// x.unwrap() is from outside the macro
|
|
|
|
assert_eq!(differing_macro_contexts(x_is_some_span, x_unwrap_span), true);
|
|
|
|
```
|
|
|
|
|
|
|
|
[TyS]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.TyS.html
|
|
|
|
[TyKind]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/enum.TyKind.html
|
2020-07-17 03:47:04 -05:00
|
|
|
[TypeckResults]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.TypeckResults.html
|
|
|
|
[expr_ty]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.TypeckResults.html#method.expr_ty
|
2020-05-28 08:45:24 -05:00
|
|
|
[LateContext]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_lint/struct.LateContext.html
|
|
|
|
[TyCtxt]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/context/struct.TyCtxt.html
|
2020-07-17 03:47:04 -05:00
|
|
|
[pat_ty]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/context/struct.TypeckResults.html#method.pat_ty
|
2021-02-18 05:08:46 -06:00
|
|
|
[paths]: ../clippy_utils/src/paths.rs
|
|
|
|
[utils]: https://github.com/rust-lang/rust-clippy/blob/master/clippy_utils/src/lib.rs
|