#![allow(unused)]fnmain() {
use rustc_hir as hir;
use rustc_lint::{LateContext, LateLintPass};
use rustc_span::sym;
use clippy_utils::is_trait_method;
impl<'tcx> LateLintPass<'tcx> for OurFancyMethodLint {
fncheck_expr(&mutself, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
// Check our expr is calling a method with pattern matchingiflet 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"// 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`)// See the next section for more information.
&& is_trait_method(cx, self_arg, sym::OurFancyTrait)
{
println!("`expr` is a method call for `our_fancy_method`");
}
}
}
}
#![allow(unused)]fnmain() {
use clippy_utils::ty::is_type_diagnostic_item;
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 {
fncheck_impl_item(&mutself, cx: &LateContext<'tcx>, impl_item: &'tcx ImplItem<'_>) {
// Check if item is a method/functioniflet ImplItemKind::Fn(ref signature, _) = impl_item.kind
// Check the method is named `our_fancy_method`
&& impl_item.ident.name.as_str() == "our_fancy_method"// We can also check it has a parameter `self`
&& signature.decl.implicit_self.has_implicit_self()
// We can go even further and even check if its return type is `String`
&& is_type_diagnostic_item(cx, return_ty(cx, impl_item.hir_id), sym::String)
{
println!("`our_fancy_method` is implemented!");
}
}
}
}