-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 #8665 - InfRandomness:option_take_on_temporary, r=llogiq
Introduce needless_option_take lint - \[x] Followed [lint naming conventions][lint_naming] - \[x] Added passing UI tests (including committed `.stderr` file) - \[x] `cargo test` passes locally - \[x] Executed `cargo dev update_lints` - \[x] Added lint documentation - \[x] Run `cargo dev fmt` Fixes #8618 changelog: Introduce [`needless_option_take`] lint
- Loading branch information
Showing
10 changed files
with
123 additions
and
0 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,41 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::match_def_path; | ||
use clippy_utils::source::snippet_with_applicability; | ||
use clippy_utils::ty::is_type_diagnostic_item; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::Expr; | ||
use rustc_lint::LateContext; | ||
use rustc_span::sym; | ||
|
||
use super::NEEDLESS_OPTION_TAKE; | ||
|
||
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, recv: &'tcx Expr<'_>) { | ||
// Checks if expression type is equal to sym::Option and if the expr is not a syntactic place | ||
if !recv.is_syntactic_place_expr() && is_expr_option(cx, recv) && has_expr_as_ref_path(cx, recv) { | ||
let mut applicability = Applicability::MachineApplicable; | ||
span_lint_and_sugg( | ||
cx, | ||
NEEDLESS_OPTION_TAKE, | ||
expr.span, | ||
"called `Option::take()` on a temporary value", | ||
"try", | ||
format!( | ||
"{}", | ||
snippet_with_applicability(cx, recv.span, "..", &mut applicability) | ||
), | ||
applicability, | ||
); | ||
} | ||
} | ||
|
||
fn is_expr_option(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool { | ||
let expr_type = cx.typeck_results().expr_ty(expr); | ||
is_type_diagnostic_item(cx, expr_type, sym::Option) | ||
} | ||
|
||
fn has_expr_as_ref_path(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool { | ||
if let Some(ref_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) { | ||
return match_def_path(cx, ref_id, &["core", "option", "Option", "as_ref"]); | ||
} | ||
false | ||
} |
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,15 @@ | ||
// run-rustfix | ||
|
||
fn main() { | ||
println!("Testing non erroneous option_take_on_temporary"); | ||
let mut option = Some(1); | ||
let _ = Box::new(move || option.take().unwrap()); | ||
|
||
println!("Testing non erroneous option_take_on_temporary"); | ||
let x = Some(3); | ||
x.as_ref(); | ||
|
||
println!("Testing erroneous option_take_on_temporary"); | ||
let x = Some(3); | ||
x.as_ref(); | ||
} |
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,15 @@ | ||
// run-rustfix | ||
|
||
fn main() { | ||
println!("Testing non erroneous option_take_on_temporary"); | ||
let mut option = Some(1); | ||
let _ = Box::new(move || option.take().unwrap()); | ||
|
||
println!("Testing non erroneous option_take_on_temporary"); | ||
let x = Some(3); | ||
x.as_ref(); | ||
|
||
println!("Testing erroneous option_take_on_temporary"); | ||
let x = Some(3); | ||
x.as_ref().take(); | ||
} |
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,10 @@ | ||
error: called `Option::take()` on a temporary value | ||
--> $DIR/needless_option_take.rs:14:5 | ||
| | ||
LL | x.as_ref().take(); | ||
| ^^^^^^^^^^^^^^^^^ help: try: `x.as_ref()` | ||
| | ||
= note: `-D clippy::needless-option-take` implied by `-D warnings` | ||
|
||
error: aborting due to previous error | ||
|
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,15 @@ | ||
// run-rustfix | ||
|
||
fn main() { | ||
println!("Testing non erroneous option_take_on_temporary"); | ||
let mut option = Some(1); | ||
let _ = Box::new(move || option.take().unwrap()); | ||
|
||
println!("Testing non erroneous option_take_on_temporary"); | ||
let x = Some(3); | ||
x.as_ref(); | ||
|
||
println!("Testing erroneous option_take_on_temporary"); | ||
let x = Some(3); | ||
x.as_ref(); | ||
} |