forked from rust-lang/rust
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#8646 - Alexendoo:option-as-deref-mut, r=giraf…
…fate Fix `as_deref_mut` false positives in `needless_option_as_deref` Also moves it into `methods/` Fixes rust-lang#7846 Fixes rust-lang#8047 changelog: [`needless_option_as_deref`]: No longer lints for `as_deref_mut` on Options that cannot be moved supersedes rust-lang#8064
- Loading branch information
Showing
10 changed files
with
136 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::path_res; | ||
use clippy_utils::source::snippet_opt; | ||
use clippy_utils::ty::is_type_diagnostic_item; | ||
use clippy_utils::usage::local_used_after_expr; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::def::Res; | ||
use rustc_hir::Expr; | ||
use rustc_lint::LateContext; | ||
use rustc_span::sym; | ||
|
||
use super::NEEDLESS_OPTION_AS_DEREF; | ||
|
||
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, name: &str) { | ||
let typeck = cx.typeck_results(); | ||
let outer_ty = typeck.expr_ty(expr); | ||
|
||
if is_type_diagnostic_item(cx, outer_ty, sym::Option) && outer_ty == typeck.expr_ty(recv) { | ||
if name == "as_deref_mut" && recv.is_syntactic_place_expr() { | ||
let Res::Local(binding_id) = path_res(cx, recv) 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 this", | ||
snippet_opt(cx, recv.span).unwrap(), | ||
Applicability::MachineApplicable, | ||
); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,41 @@ | ||
// run-rustfix | ||
|
||
#[warn(clippy::needless_option_as_deref)] | ||
#![allow(unused)] | ||
#![warn(clippy::needless_option_as_deref)] | ||
|
||
fn main() { | ||
// should lint | ||
let _: Option<&usize> = Some(&1); | ||
let _: Option<&mut usize> = Some(&mut 1); | ||
|
||
let mut y = 0; | ||
let mut x = Some(&mut y); | ||
let _ = x; | ||
|
||
// 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(); | ||
|
||
let mut i = 0; | ||
let x = &mut Some(&mut i); | ||
(*x).as_deref_mut(); | ||
|
||
// #8047 | ||
let mut y = 0; | ||
let mut x = Some(&mut y); | ||
x.as_deref_mut(); | ||
dbg!(x); | ||
} | ||
|
||
struct S<'a> { | ||
opt: Option<&'a mut usize>, | ||
} | ||
|
||
fn from_field<'a>(s: &'a mut S<'a>) -> Option<&'a mut usize> { | ||
s.opt.as_deref_mut() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,41 @@ | ||
// run-rustfix | ||
|
||
#[warn(clippy::needless_option_as_deref)] | ||
#![allow(unused)] | ||
#![warn(clippy::needless_option_as_deref)] | ||
|
||
fn main() { | ||
// should lint | ||
let _: Option<&usize> = Some(&1).as_deref(); | ||
let _: Option<&mut usize> = Some(&mut 1).as_deref_mut(); | ||
|
||
let mut y = 0; | ||
let mut x = Some(&mut y); | ||
let _ = x.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(); | ||
|
||
let mut i = 0; | ||
let x = &mut Some(&mut i); | ||
(*x).as_deref_mut(); | ||
|
||
// #8047 | ||
let mut y = 0; | ||
let mut x = Some(&mut y); | ||
x.as_deref_mut(); | ||
dbg!(x); | ||
} | ||
|
||
struct S<'a> { | ||
opt: Option<&'a mut usize>, | ||
} | ||
|
||
fn from_field<'a>(s: &'a mut S<'a>) -> Option<&'a mut usize> { | ||
s.opt.as_deref_mut() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,22 @@ | ||
error: derefed type is same as origin | ||
--> $DIR/needless_option_as_deref.rs:7:29 | ||
--> $DIR/needless_option_as_deref.rs:8:29 | ||
| | ||
LL | let _: Option<&usize> = Some(&1).as_deref(); | ||
| ^^^^^^^^^^^^^^^^^^^ help: try this: `Some(&1)` | ||
| | ||
= 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 | ||
--> $DIR/needless_option_as_deref.rs:9: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: derefed type is same as origin | ||
--> $DIR/needless_option_as_deref.rs:13:13 | ||
| | ||
LL | let _ = x.as_deref_mut(); | ||
| ^^^^^^^^^^^^^^^^ help: try this: `x` | ||
|
||
error: aborting due to 3 previous errors | ||
|