-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New lint: option_needless_deref #7596
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,69 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::in_macro; | ||
use clippy_utils::source::snippet_opt; | ||
use clippy_utils::ty::is_type_diagnostic_item; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Expr, ExprKind}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_middle::ty::TyS; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
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. | ||
/// | ||
/// ### Why is this bad? | ||
/// Redundant code and improving readability. | ||
/// | ||
/// ### Example | ||
/// ```rust | ||
/// let a = Some(&1); | ||
/// let b = a.as_deref(); // goes from Option<&i32> to Option<&i32> | ||
/// ``` | ||
/// Could be written as: | ||
/// ```rust | ||
/// let a = Some(&1); | ||
/// let b = a; | ||
/// ``` | ||
pub NEEDLESS_OPTION_AS_DEREF, | ||
complexity, | ||
"no-op use of `deref` or `deref_mut` method to `Option`." | ||
} | ||
|
||
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<'_>) { | ||
if expr.span.from_expansion() { | ||
return; | ||
} | ||
if in_macro(expr.span) { | ||
return; | ||
} | ||
let typeck = cx.typeck_results(); | ||
let outer_ty = typeck.expr_ty(expr); | ||
|
||
if_chain! { | ||
if is_type_diagnostic_item(cx,outer_ty,sym::option_type); | ||
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{ | ||
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(), | ||
Applicability::MachineApplicable | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or 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,13 @@ | ||
// run-rustfix | ||
|
||
#[warn(clippy::needless_option_as_deref)] | ||
|
||
fn main() { | ||
// should lint | ||
let _: Option<&usize> = Some(&1); | ||
let _: Option<&mut usize> = Some(&mut 1); | ||
|
||
// should not lint | ||
let _ = Some(Box::new(1)).as_deref(); | ||
let _ = Some(Box::new(1)).as_deref_mut(); | ||
} |
This file contains hidden or 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,13 @@ | ||
// run-rustfix | ||
|
||
#[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(); | ||
|
||
// should not lint | ||
let _ = Some(Box::new(1)).as_deref(); | ||
let _ = Some(Box::new(1)).as_deref_mut(); | ||
} |
This file contains hidden or 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,16 @@ | ||
error: derefed type is same as origin | ||
--> $DIR/needless_option_as_deref.rs:7: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 | ||
| | ||
LL | let _: Option<&mut usize> = Some(&mut 1).as_deref_mut(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `Some(&mut 1)` | ||
|
||
error: aborting due to 2 previous errors | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.