Skip to content

Commit 38b8697

Browse files
committed
option_needless_deref
1 parent a8c2c7b commit 38b8697

7 files changed

+118
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -2848,6 +2848,7 @@ Released 2018-09-13
28482848
[`option_if_let_else`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_if_let_else
28492849
[`option_map_or_none`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_map_or_none
28502850
[`option_map_unit_fn`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_map_unit_fn
2851+
[`option_needless_deref`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_needless_deref
28512852
[`option_option`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_option
28522853
[`or_fun_call`]: https://rust-lang.github.io/rust-clippy/master/index.html#or_fun_call
28532854
[`out_of_bounds_indexing`]: https://rust-lang.github.io/rust-clippy/master/index.html#out_of_bounds_indexing

clippy_lints/src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ mod nonstandard_macro_braces;
303303
mod open_options;
304304
mod option_env_unwrap;
305305
mod option_if_let_else;
306+
mod option_needless_deref;
306307
mod overflow_check_conditional;
307308
mod panic_in_result_fn;
308309
mod panic_unimplemented;
@@ -865,6 +866,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
865866
open_options::NONSENSICAL_OPEN_OPTIONS,
866867
option_env_unwrap::OPTION_ENV_UNWRAP,
867868
option_if_let_else::OPTION_IF_LET_ELSE,
869+
option_needless_deref::OPTION_NEEDLESS_DEREF,
868870
overflow_check_conditional::OVERFLOW_CHECK_CONDITIONAL,
869871
panic_in_result_fn::PANIC_IN_RESULT_FN,
870872
panic_unimplemented::PANIC,
@@ -1388,6 +1390,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
13881390
LintId::of(non_octal_unix_permissions::NON_OCTAL_UNIX_PERMISSIONS),
13891391
LintId::of(open_options::NONSENSICAL_OPEN_OPTIONS),
13901392
LintId::of(option_env_unwrap::OPTION_ENV_UNWRAP),
1393+
LintId::of(option_needless_deref::OPTION_NEEDLESS_DEREF),
13911394
LintId::of(overflow_check_conditional::OVERFLOW_CHECK_CONDITIONAL),
13921395
LintId::of(partialeq_ne_impl::PARTIALEQ_NE_IMPL),
13931396
LintId::of(precedence::PRECEDENCE),
@@ -1641,6 +1644,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
16411644
LintId::of(neg_cmp_op_on_partial_ord::NEG_CMP_OP_ON_PARTIAL_ORD),
16421645
LintId::of(no_effect::NO_EFFECT),
16431646
LintId::of(no_effect::UNNECESSARY_OPERATION),
1647+
LintId::of(option_needless_deref::OPTION_NEEDLESS_DEREF),
16441648
LintId::of(overflow_check_conditional::OVERFLOW_CHECK_CONDITIONAL),
16451649
LintId::of(partialeq_ne_impl::PARTIALEQ_NE_IMPL),
16461650
LintId::of(precedence::PRECEDENCE),
@@ -1863,6 +1867,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
18631867
store.register_late_pass(|| Box::new(ptr::Ptr));
18641868
store.register_late_pass(|| Box::new(ptr_eq::PtrEq));
18651869
store.register_late_pass(|| Box::new(needless_bool::NeedlessBool));
1870+
store.register_late_pass(|| Box::new(option_needless_deref::OptionNeedlessDeref));
18661871
store.register_late_pass(|| Box::new(needless_bool::BoolComparison));
18671872
store.register_late_pass(|| Box::new(needless_for_each::NeedlessForEach));
18681873
store.register_late_pass(|| Box::new(approx_const::ApproxConstant));

clippy_lints/src/loops/never_loop.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ fn combine_branches(b1: NeverLoopResult, b2: NeverLoopResult) -> NeverLoopResult
8787

8888
fn never_loop_block(block: &Block<'_>, main_loop_id: HirId) -> NeverLoopResult {
8989
let stmts = block.stmts.iter().map(stmt_to_expr);
90-
let expr = once(block.expr.as_deref());
90+
let expr = once(block.expr);
9191
let mut iter = stmts.chain(expr).flatten();
9292
never_loop_expr_seq(&mut iter, main_loop_id)
9393
}
@@ -100,7 +100,7 @@ fn never_loop_expr_seq<'a, T: Iterator<Item = &'a Expr<'a>>>(es: &mut T, main_lo
100100
fn stmt_to_expr<'tcx>(stmt: &Stmt<'tcx>) -> Option<&'tcx Expr<'tcx>> {
101101
match stmt.kind {
102102
StmtKind::Semi(e, ..) | StmtKind::Expr(e, ..) => Some(e),
103-
StmtKind::Local(local) => local.init.as_deref(),
103+
StmtKind::Local(local) => local.init,
104104
StmtKind::Item(..) => None,
105105
}
106106
}
+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::in_macro;
3+
use clippy_utils::source::snippet_opt;
4+
use clippy_utils::ty::is_type_diagnostic_item;
5+
use rustc_errors::Applicability;
6+
use rustc_hir::{Expr, ExprKind};
7+
use rustc_lint::{LateContext, LateLintPass};
8+
use rustc_middle::ty::TyS;
9+
use rustc_session::{declare_lint_pass, declare_tool_lint};
10+
use rustc_span::symbol::sym;
11+
12+
declare_clippy_lint! {
13+
/// ### What it does
14+
/// Checks for no-op uses of {Option,Result}::{as_deref,as_deref_mut}.
15+
///
16+
/// ### Why is this bad?
17+
/// Removes useless code, removing clutter and improving readability.
18+
///
19+
/// ### Example
20+
/// ```rust
21+
/// let a = Some(&1);
22+
/// let b = a.as_deref(); // goes from Option<&i32> to Option<&i32>
23+
/// ```
24+
/// Could be written as:
25+
/// ```rust
26+
/// let a = Some(&1);
27+
/// let b = a;
28+
/// ```
29+
pub OPTION_NEEDLESS_DEREF,
30+
complexity,
31+
"Explicit use of deref or deref_mut method while not in a method chain."
32+
}
33+
34+
declare_lint_pass!(OptionNeedlessDeref=> [
35+
OPTION_NEEDLESS_DEREF,
36+
]);
37+
38+
impl<'tcx> LateLintPass<'tcx> for OptionNeedlessDeref {
39+
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
40+
if expr.span.from_expansion() {
41+
return;
42+
}
43+
if in_macro(expr.span) {
44+
return;
45+
}
46+
let typeck = cx.typeck_results();
47+
let outer_ty = typeck.expr_ty(expr);
48+
49+
if_chain! {
50+
if is_type_diagnostic_item(cx,outer_ty,sym::option_type);
51+
if let ExprKind::MethodCall(path, _, [sub_expr], _) = expr.kind;
52+
let symbol = path.ident.as_str();
53+
if symbol=="as_deref" || symbol=="as_deref_mut";
54+
if TyS::same_type( outer_ty, typeck.expr_ty(sub_expr) );
55+
then{
56+
span_lint_and_sugg(
57+
cx,
58+
OPTION_NEEDLESS_DEREF,
59+
expr.span,
60+
"derefed type is same as origin",
61+
"try this",
62+
snippet_opt(cx,sub_expr.span).unwrap(),
63+
Applicability::MachineApplicable
64+
);
65+
}
66+
}
67+
}
68+
}

tests/ui/option_needless_deref.fixed

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// run-rustfix
2+
3+
#[warn(clippy::option_needless_deref)]
4+
5+
fn main() {
6+
// should lint
7+
let _: Option<&usize> = Some(&1);
8+
let _: Option<&mut usize> = Some(&mut 1);
9+
10+
// should not lint
11+
let _ = Some(Box::new(1)).as_deref();
12+
let _ = Some(Box::new(1)).as_deref_mut();
13+
}

tests/ui/option_needless_deref.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// run-rustfix
2+
3+
#[warn(clippy::option_needless_deref)]
4+
5+
fn main() {
6+
// should lint
7+
let _: Option<&usize> = Some(&1).as_deref();
8+
let _: Option<&mut usize> = Some(&mut 1).as_deref_mut();
9+
10+
// should not lint
11+
let _ = Some(Box::new(1)).as_deref();
12+
let _ = Some(Box::new(1)).as_deref_mut();
13+
}

tests/ui/option_needless_deref.stderr

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: derefed type is same as origin
2+
--> $DIR/option_needless_deref.rs:7:29
3+
|
4+
LL | let _: Option<&usize> = Some(&1).as_deref();
5+
| ^^^^^^^^^^^^^^^^^^^ help: try this: `Some(&1)`
6+
|
7+
= note: `-D clippy::option-needless-deref` implied by `-D warnings`
8+
9+
error: derefed type is same as origin
10+
--> $DIR/option_needless_deref.rs:8:33
11+
|
12+
LL | let _: Option<&mut usize> = Some(&mut 1).as_deref_mut();
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `Some(&mut 1)`
14+
15+
error: aborting due to 2 previous errors
16+

0 commit comments

Comments
 (0)