-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathneedless_option_as_deref.rs
More file actions
39 lines (34 loc) · 1.2 KB
/
needless_option_as_deref.rs
File metadata and controls
39 lines (34 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::res::{MaybeDef, MaybeResPath};
use clippy_utils::source::SpanRangeExt;
use clippy_utils::sym;
use clippy_utils::usage::local_used_after_expr;
use rustc_errors::Applicability;
use rustc_hir::Expr;
use rustc_hir::def::Res;
use rustc_lint::LateContext;
use rustc_span::Symbol;
use super::NEEDLESS_OPTION_AS_DEREF;
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, name: Symbol) {
let typeck = cx.typeck_results();
let outer_ty = typeck.expr_ty(expr);
if outer_ty.is_diag_item(cx, sym::Option) && outer_ty == typeck.expr_ty(recv) {
if name == sym::as_deref_mut && recv.is_syntactic_place_expr() {
let Res::Local(binding_id) = *recv.basic_res() else {
return;
};
if local_used_after_expr(cx, binding_id, recv) {
return;
}
}
span_lint_and_sugg(
cx,
NEEDLESS_OPTION_AS_DEREF,
expr.span,
"derefed type is same as origin",
"try",
recv.span.get_source_text(cx).unwrap().to_owned(),
Applicability::MachineApplicable,
);
}
}