diff options
| author | xFrednet <xFrednet@gmail.com> | 2021-07-14 22:10:15 +0200 |
|---|---|---|
| committer | xFrednet <xFrednet@gmail.com> | 2021-07-14 22:50:59 +0200 |
| commit | 6030428fd22912afeeaecfcc0220e979c2ffeb0a (patch) | |
| tree | b1f6d3fd65014a76efbedf46bdd327bfa3103005 /clippy_utils/src/lib.rs | |
| parent | 2ac21889bcc0634eabd1ff36491042fd0dae003f (diff) | |
| download | rust-6030428fd22912afeeaecfcc0220e979c2ffeb0a.tar.gz rust-6030428fd22912afeeaecfcc0220e979c2ffeb0a.zip | |
Use diagnostic items for `Into`, `IntoIterator`, `LinkedList`, `ptr::null`, `prt::null_mut`
Diffstat (limited to 'clippy_utils/src/lib.rs')
| -rw-r--r-- | clippy_utils/src/lib.rs | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/clippy_utils/src/lib.rs b/clippy_utils/src/lib.rs index 364ae536ecd..b19d1430b6e 100644 --- a/clippy_utils/src/lib.rs +++ b/clippy_utils/src/lib.rs @@ -411,12 +411,22 @@ pub fn is_qpath_def_path(cx: &LateContext<'_>, path: &QPath<'_>, hir_id: HirId, } /// If the expression is a path, resolves it to a `DefId` and checks if it matches the given path. +/// +/// Please use `is_expr_diagnostic_item` if the target is a diagnostic item. pub fn is_expr_path_def_path(cx: &LateContext<'_>, expr: &Expr<'_>, segments: &[&str]) -> bool { expr_path_res(cx, expr) .opt_def_id() .map_or(false, |id| match_def_path(cx, id, segments)) } +/// If the expression is a path, resolves it to a `DefId` and checks if it matches the given +/// diagnostic item. +pub fn is_expr_diagnostic_item(cx: &LateContext<'_>, expr: &Expr<'_>, diag_item: Symbol) -> bool { + expr_path_res(cx, expr) + .opt_def_id() + .map_or(false, |id| cx.tcx.is_diagnostic_item(diag_item, id)) +} + /// THIS METHOD IS DEPRECATED and will eventually be removed since it does not match against the /// entire path or resolved `DefId`. Prefer using `match_def_path`. Consider getting a `DefId` from /// `QPath::Resolved.1.res.opt_def_id()`. @@ -1249,6 +1259,8 @@ pub fn match_function_call<'tcx>( /// Checks if the given `DefId` matches any of the paths. Returns the index of matching path, if /// any. +/// +/// Please use `match_any_diagnostic_items` if the targets are all diagnostic items. pub fn match_any_def_paths(cx: &LateContext<'_>, did: DefId, paths: &[&[&str]]) -> Option<usize> { let search_path = cx.get_def_path(did); paths @@ -1256,6 +1268,14 @@ pub fn match_any_def_paths(cx: &LateContext<'_>, did: DefId, paths: &[&[&str]]) .position(|p| p.iter().map(|x| Symbol::intern(x)).eq(search_path.iter().copied())) } +/// Checks if the given `DefId` matches any of provided diagnostic items. Returns the index of +/// matching path, if any. +pub fn match_any_diagnostic_items(cx: &LateContext<'_>, def_id: DefId, diag_items: &[Symbol]) -> Option<usize> { + diag_items + .iter() + .position(|item| cx.tcx.is_diagnostic_item(*item, def_id)) +} + /// Checks if the given `DefId` matches the path. pub fn match_def_path<'tcx>(cx: &LateContext<'tcx>, did: DefId, syms: &[&str]) -> bool { // We should probably move to Symbols in Clippy as well rather than interning every time. |
