Skip to content

Commit 76268c0

Browse files
committed
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
1 parent b52bc9b commit 76268c0

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

clippy_lints/src/methods/needless_option_take.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::match_def_path;
23
use clippy_utils::source::snippet_with_applicability;
34
use clippy_utils::ty::is_type_diagnostic_item;
45
use rustc_errors::Applicability;
@@ -10,7 +11,7 @@ use super::NEEDLESS_OPTION_TAKE;
1011

1112
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, recv: &'tcx Expr<'_>) {
1213
// Checks if expression type is equal to sym::Option and if the expr is not a syntactic place
13-
if is_expr_option(cx, recv) && !recv.is_syntactic_place_expr() {
14+
if !recv.is_syntactic_place_expr() && is_expr_option(cx, recv) && has_expr_as_ref_path(cx, recv) {
1415
let mut applicability = Applicability::MachineApplicable;
1516
span_lint_and_sugg(
1617
cx,
@@ -31,3 +32,10 @@ fn is_expr_option(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
3132
let expr_type = cx.typeck_results().expr_ty(expr);
3233
is_type_diagnostic_item(cx, expr_type, sym::Option)
3334
}
35+
36+
fn has_expr_as_ref_path(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
37+
if let Some(ref_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) {
38+
return match_def_path(cx, ref_id, &["core", "option", "Option", "as_ref"]);
39+
}
40+
false
41+
}

0 commit comments

Comments
 (0)