Skip to content
/ rustc Public
forked from rust-lang/rust

Commit 5699672

Browse files
committed
Auto merge of rust-lang#5674 - ThibsG:DocTypeDefinesMethod, r=yaahc,phansch,flip1995
Add doc for checking if type defines specific method This PR adds documentation on how: - check if a type defines a specific method - check an expr is calling a specific method closes: rust-lang#3843 changelog: none
2 parents e6f05ad + 9e89ba9 commit 5699672

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

doc/common_tools_writing_lints.md

+48
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ You may need following tooltips to catch up with common operations.
44

55
- [Common tools for writing lints](#common-tools-for-writing-lints)
66
- [Retrieving the type of an expression](#retrieving-the-type-of-an-expression)
7+
- [Checking if an expression is calling a specific method](#checking-if-an-expr-is-calling-a-specific-method)
78
- [Checking if a type implements a specific trait](#checking-if-a-type-implements-a-specific-trait)
9+
- [Checking if a type defines a method](#checking-if-a-type-defines-a-method)
810
- [Dealing with macros](#dealing-with-macros)
911

1012
Useful Rustc dev guide links:
@@ -49,6 +51,26 @@ Two noticeable items here:
4951
- `tables` is [`TypeckTables`][TypeckTables] and is created by type checking step,
5052
it includes useful information such as types of expressions, ways to resolve methods and so on.
5153

54+
# Checking if an expr is calling a specific method
55+
56+
Starting with an `expr`, you can check whether it is calling a specific method `some_method`:
57+
58+
```rust
59+
impl LateLintPass<'_, '_> for MyStructLint {
60+
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr<'_>) {
61+
if_chain! {
62+
// Check our expr is calling a method
63+
if let hir::ExprKind::MethodCall(path, _, _args) = &expr.kind;
64+
// Check the name of this method is `some_method`
65+
if path.ident.name == sym!(some_method);
66+
then {
67+
// ...
68+
}
69+
}
70+
}
71+
}
72+
```
73+
5274
# Checking if a type implements a specific trait
5375

5476
There are two ways to do this, depending if the target trait is part of lang items.
@@ -83,6 +105,32 @@ A list of defined paths for Clippy can be found in [paths.rs][paths]
83105

84106
We access lang items through the type context `tcx`. `tcx` is of type [`TyCtxt`][TyCtxt] and is defined in the `rustc_middle` crate.
85107

108+
# Checking if a type defines a specific method
109+
110+
To check if our type defines a method called `some_method`:
111+
112+
```rust
113+
use crate::utils::{is_type_diagnostic_item, return_ty};
114+
115+
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MyTypeImpl {
116+
fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, impl_item: &'tcx ImplItem<'_>) {
117+
if_chain! {
118+
// Check if item is a method/function
119+
if let ImplItemKind::Fn(ref signature, _) = impl_item.kind;
120+
// Check the method is named `some_method`
121+
if impl_item.ident.name == sym!(some_method);
122+
// We can also check it has a parameter `self`
123+
if signature.decl.implicit_self.has_implicit_self();
124+
// We can go further and even check if its return type is `String`
125+
if is_type_diagnostic_item(cx, return_ty(cx, impl_item.hir_id), sym!(string_type));
126+
then {
127+
// ...
128+
}
129+
}
130+
}
131+
}
132+
```
133+
86134
# Dealing with macros
87135

88136
There are several helpers in Clippy's utils to deal with macros:

0 commit comments

Comments
 (0)