Skip to content

Ignore as_deref_mut in needless_option_as_deref #8064

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions clippy_lints/src/needless_option_as_deref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use rustc_span::symbol::sym;

declare_clippy_lint! {
/// ### What it does
/// Checks for no-op uses of Option::{as_deref,as_deref_mut},
/// for example, `Option<&T>::as_deref()` returns the same type.
/// Checks for no-op uses of `Option::as_deref`, for example,
/// `Option<&T>::as_deref()` returns the same type.
///
/// ### Why is this bad?
/// Redundant code and improving readability.
Expand All @@ -29,12 +29,10 @@ declare_clippy_lint! {
#[clippy::version = "1.57.0"]
pub NEEDLESS_OPTION_AS_DEREF,
complexity,
"no-op use of `deref` or `deref_mut` method to `Option`."
"no-op use of `as_deref` method to `Option`."
}

declare_lint_pass!(OptionNeedlessDeref=> [
NEEDLESS_OPTION_AS_DEREF,
]);
declare_lint_pass!(OptionNeedlessDeref => [NEEDLESS_OPTION_AS_DEREF]);

impl<'tcx> LateLintPass<'tcx> for OptionNeedlessDeref {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
Expand All @@ -45,19 +43,18 @@ impl<'tcx> LateLintPass<'tcx> for OptionNeedlessDeref {
let outer_ty = typeck.expr_ty(expr);

if_chain! {
if is_type_diagnostic_item(cx,outer_ty,sym::Option);
if is_type_diagnostic_item(cx, outer_ty, sym::Option);
if let ExprKind::MethodCall(path, _, [sub_expr], _) = expr.kind;
let symbol = path.ident.as_str();
if symbol == "as_deref" || symbol == "as_deref_mut";
if TyS::same_type( outer_ty, typeck.expr_ty(sub_expr) );
then{
if path.ident.as_str() == "as_deref";
if TyS::same_type(outer_ty, typeck.expr_ty(sub_expr));
then {
span_lint_and_sugg(
cx,
NEEDLESS_OPTION_AS_DEREF,
expr.span,
"derefed type is same as origin",
"try this",
snippet_opt(cx,sub_expr.span).unwrap(),
snippet_opt(cx, sub_expr.span).unwrap(),
Applicability::MachineApplicable
);
}
Expand Down
15 changes: 14 additions & 1 deletion tests/ui/needless_option_as_deref.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,22 @@
fn main() {
// should lint
let _: Option<&usize> = Some(&1);
let _: Option<&mut usize> = Some(&mut 1);

// false negative, could lint if the source Option is movable and not used later
let _: Option<&mut usize> = Some(&mut 1).as_deref_mut();

// should not lint
let _ = Some(Box::new(1)).as_deref();
let _ = Some(Box::new(1)).as_deref_mut();

// #7846
let mut i = 0;
let mut opt_vec = vec![Some(&mut i)];
opt_vec[0].as_deref_mut().unwrap();

// #8047
let mut y = 0;
let mut x = Some(&mut y);
x.as_deref_mut();
println!("{:?}", x);
}
13 changes: 13 additions & 0 deletions tests/ui/needless_option_as_deref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,22 @@
fn main() {
// should lint
let _: Option<&usize> = Some(&1).as_deref();

// false negative, could lint if the source Option is movable and not used later
let _: Option<&mut usize> = Some(&mut 1).as_deref_mut();

// should not lint
let _ = Some(Box::new(1)).as_deref();
let _ = Some(Box::new(1)).as_deref_mut();

// #7846
let mut i = 0;
let mut opt_vec = vec![Some(&mut i)];
opt_vec[0].as_deref_mut().unwrap();

// #8047
let mut y = 0;
let mut x = Some(&mut y);
x.as_deref_mut();
println!("{:?}", x);
}
8 changes: 1 addition & 7 deletions tests/ui/needless_option_as_deref.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,5 @@ LL | let _: Option<&usize> = Some(&1).as_deref();
|
= note: `-D clippy::needless-option-as-deref` implied by `-D warnings`

error: derefed type is same as origin
--> $DIR/needless_option_as_deref.rs:8:33
|
LL | let _: Option<&mut usize> = Some(&mut 1).as_deref_mut();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `Some(&mut 1)`

error: aborting due to 2 previous errors
error: aborting due to previous error