diff options
| author | infrandomness <infrandomness@gmail.com> | 2022-04-10 23:42:33 +0200 |
|---|---|---|
| committer | infrandomness <infrandomness@gmail.com> | 2022-04-14 13:16:20 +0200 |
| commit | 76268c0d5590ca06129722fed4afb306a847caab (patch) | |
| tree | 2662c6926ad488643224a86cdb89a550fee7f0ee | |
| parent | b52bc9b96bb3b22b4ab4a793e3706e7a9b718952 (diff) | |
| download | rust-76268c0d5590ca06129722fed4afb306a847caab.tar.gz rust-76268c0d5590ca06129722fed4afb306a847caab.zip | |
Introduce new lint check
This checks if the expression has one of `core`, `option`, `Option` or `as_ref` in its path, this avoids false positives
| -rw-r--r-- | clippy_lints/src/methods/needless_option_take.rs | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/clippy_lints/src/methods/needless_option_take.rs b/clippy_lints/src/methods/needless_option_take.rs index 297d83e9e37..5596515637e 100644 --- a/clippy_lints/src/methods/needless_option_take.rs +++ b/clippy_lints/src/methods/needless_option_take.rs @@ -1,4 +1,5 @@ use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::match_def_path; use clippy_utils::source::snippet_with_applicability; use clippy_utils::ty::is_type_diagnostic_item; use rustc_errors::Applicability; @@ -10,7 +11,7 @@ use super::NEEDLESS_OPTION_TAKE; pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, recv: &'tcx Expr<'_>) { // Checks if expression type is equal to sym::Option and if the expr is not a syntactic place - if is_expr_option(cx, recv) && !recv.is_syntactic_place_expr() { + if !recv.is_syntactic_place_expr() && is_expr_option(cx, recv) && has_expr_as_ref_path(cx, recv) { let mut applicability = Applicability::MachineApplicable; span_lint_and_sugg( cx, @@ -31,3 +32,10 @@ fn is_expr_option(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool { let expr_type = cx.typeck_results().expr_ty(expr); is_type_diagnostic_item(cx, expr_type, sym::Option) } + +fn has_expr_as_ref_path(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool { + if let Some(ref_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) { + return match_def_path(cx, ref_id, &["core", "option", "Option", "as_ref"]); + } + false +} |
