-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
manual_is_variant_and
lint
- Loading branch information
Showing
11 changed files
with
290 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use clippy_config::msrvs::{Msrv, OPTION_RESULT_IS_VARIANT_AND}; | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::source::snippet; | ||
use clippy_utils::ty::is_type_diagnostic_item; | ||
use rustc_errors::Applicability; | ||
use rustc_lint::LateContext; | ||
use rustc_span::{sym, Span}; | ||
|
||
use super::MANUAL_IS_VARIANT_AND; | ||
|
||
pub(super) fn check<'tcx>( | ||
cx: &LateContext<'_>, | ||
expr: &'tcx rustc_hir::Expr<'_>, | ||
map_recv: &'tcx rustc_hir::Expr<'_>, | ||
map_arg: &'tcx rustc_hir::Expr<'_>, | ||
map_span: Span, | ||
msrv: &Msrv, | ||
) { | ||
// Don't lint if: | ||
|
||
// 1. the `expr` is generated by a macro | ||
if expr.span.from_expansion() { | ||
return; | ||
} | ||
|
||
// 2. the caller of `map()` is neither `Option` nor `Result` | ||
let is_option = is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(map_recv), sym::Option); | ||
let is_result = is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(map_recv), sym::Result); | ||
if !is_option && !is_result { | ||
return; | ||
} | ||
|
||
// 3. the caller of `unwrap_or_default` is neither `Option<bool>` nor `Result<bool, _>` | ||
if !cx.typeck_results().expr_ty(expr).is_bool() { | ||
return; | ||
} | ||
|
||
// 4. msrv doesn't meet `OPTION_RESULT_IS_VARIANT_AND` | ||
if !msrv.meets(OPTION_RESULT_IS_VARIANT_AND) { | ||
return; | ||
} | ||
|
||
let lint_msg = if is_option { | ||
"called `map(<f>).unwrap_or_default()` on an `Option` value" | ||
} else { | ||
"called `map(<f>).unwrap_or_default()` on a `Result` value" | ||
}; | ||
let suggestion = if is_option { "is_some_and" } else { "is_ok_and" }; | ||
|
||
span_lint_and_sugg( | ||
cx, | ||
MANUAL_IS_VARIANT_AND, | ||
expr.span.with_lo(map_span.lo()), | ||
lint_msg, | ||
"use", | ||
format!("{}({})", suggestion, snippet(cx, map_arg.span, "..")), | ||
Applicability::MachineApplicable, | ||
); | ||
} |
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,51 @@ | ||
//@aux-build:option_helpers.rs | ||
#![warn(clippy::manual_is_variant_and)] | ||
|
||
#[macro_use] | ||
extern crate option_helpers; | ||
|
||
#[rustfmt::skip] | ||
fn option_methods() { | ||
let opt = Some(1); | ||
|
||
// Check for `option.map(_).unwrap_or_default()` use. | ||
// Single line case. | ||
let _ = opt.is_some_and(|x| x > 1); | ||
// Multi-line cases. | ||
let _ = opt.is_some_and(|x| { | ||
x > 1 | ||
}); | ||
let _ = opt.is_some_and(|x| x > 1); | ||
let _ = opt | ||
.is_some_and(|x| x > 1); | ||
|
||
// won't fix because the return type of the closure is not `bool` | ||
let _ = opt.map(|x| x + 1).unwrap_or_default(); | ||
|
||
let opt2 = Some('a'); | ||
let _ = opt2.is_some_and(char::is_alphanumeric); // should lint | ||
let _ = opt_map!(opt2, |x| x == 'a').unwrap_or_default(); // should not lint | ||
} | ||
|
||
#[rustfmt::skip] | ||
fn result_methods() { | ||
let res: Result<i32, ()> = Ok(1); | ||
|
||
// multi line cases | ||
let _ = res.is_ok_and(|x| { | ||
x > 1 | ||
}); | ||
let _ = res.is_ok_and(|x| x > 1); | ||
|
||
// won't fix because the return type of the closure is not `bool` | ||
let _ = res.map(|x| x + 1).unwrap_or_default(); | ||
|
||
let res2: Result<char, ()> = Ok('a'); | ||
let _ = res2.is_ok_and(char::is_alphanumeric); // should lint | ||
let _ = opt_map!(res2, |x| x == 'a').unwrap_or_default(); // should not lint | ||
} | ||
|
||
fn main() { | ||
option_methods(); | ||
result_methods(); | ||
} |
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,57 @@ | ||
//@aux-build:option_helpers.rs | ||
#![warn(clippy::manual_is_variant_and)] | ||
|
||
#[macro_use] | ||
extern crate option_helpers; | ||
|
||
#[rustfmt::skip] | ||
fn option_methods() { | ||
let opt = Some(1); | ||
|
||
// Check for `option.map(_).unwrap_or_default()` use. | ||
// Single line case. | ||
let _ = opt.map(|x| x > 1) | ||
// Should lint even though this call is on a separate line. | ||
.unwrap_or_default(); | ||
// Multi-line cases. | ||
let _ = opt.map(|x| { | ||
x > 1 | ||
} | ||
).unwrap_or_default(); | ||
let _ = opt.map(|x| x > 1).unwrap_or_default(); | ||
let _ = opt | ||
.map(|x| x > 1) | ||
.unwrap_or_default(); | ||
|
||
// won't fix because the return type of the closure is not `bool` | ||
let _ = opt.map(|x| x + 1).unwrap_or_default(); | ||
|
||
let opt2 = Some('a'); | ||
let _ = opt2.map(char::is_alphanumeric).unwrap_or_default(); // should lint | ||
let _ = opt_map!(opt2, |x| x == 'a').unwrap_or_default(); // should not lint | ||
} | ||
|
||
#[rustfmt::skip] | ||
fn result_methods() { | ||
let res: Result<i32, ()> = Ok(1); | ||
|
||
// multi line cases | ||
let _ = res.map(|x| { | ||
x > 1 | ||
} | ||
).unwrap_or_default(); | ||
let _ = res.map(|x| x > 1) | ||
.unwrap_or_default(); | ||
|
||
// won't fix because the return type of the closure is not `bool` | ||
let _ = res.map(|x| x + 1).unwrap_or_default(); | ||
|
||
let res2: Result<char, ()> = Ok('a'); | ||
let _ = res2.map(char::is_alphanumeric).unwrap_or_default(); // should lint | ||
let _ = opt_map!(res2, |x| x == 'a').unwrap_or_default(); // should not lint | ||
} | ||
|
||
fn main() { | ||
option_methods(); | ||
result_methods(); | ||
} |
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,82 @@ | ||
error: called `map(<f>).unwrap_or_default()` on an `Option` value | ||
--> $DIR/manual_is_variant_and.rs:13:17 | ||
| | ||
LL | let _ = opt.map(|x| x > 1) | ||
| _________________^ | ||
LL | | // Should lint even though this call is on a separate line. | ||
LL | | .unwrap_or_default(); | ||
| |____________________________^ help: use: `is_some_and(|x| x > 1)` | ||
| | ||
= note: `-D clippy::manual-is-variant-and` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::manual_is_variant_and)]` | ||
|
||
error: called `map(<f>).unwrap_or_default()` on an `Option` value | ||
--> $DIR/manual_is_variant_and.rs:17:17 | ||
| | ||
LL | let _ = opt.map(|x| { | ||
| _________________^ | ||
LL | | x > 1 | ||
LL | | } | ||
LL | | ).unwrap_or_default(); | ||
| |_________________________^ | ||
| | ||
help: use | ||
| | ||
LL ~ let _ = opt.is_some_and(|x| { | ||
LL + x > 1 | ||
LL ~ }); | ||
| | ||
|
||
error: called `map(<f>).unwrap_or_default()` on an `Option` value | ||
--> $DIR/manual_is_variant_and.rs:21:17 | ||
| | ||
LL | let _ = opt.map(|x| x > 1).unwrap_or_default(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `is_some_and(|x| x > 1)` | ||
|
||
error: called `map(<f>).unwrap_or_default()` on an `Option` value | ||
--> $DIR/manual_is_variant_and.rs:23:10 | ||
| | ||
LL | .map(|x| x > 1) | ||
| __________^ | ||
LL | | .unwrap_or_default(); | ||
| |____________________________^ help: use: `is_some_and(|x| x > 1)` | ||
|
||
error: called `map(<f>).unwrap_or_default()` on an `Option` value | ||
--> $DIR/manual_is_variant_and.rs:30:18 | ||
| | ||
LL | let _ = opt2.map(char::is_alphanumeric).unwrap_or_default(); // should lint | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `is_some_and(char::is_alphanumeric)` | ||
|
||
error: called `map(<f>).unwrap_or_default()` on a `Result` value | ||
--> $DIR/manual_is_variant_and.rs:39:17 | ||
| | ||
LL | let _ = res.map(|x| { | ||
| _________________^ | ||
LL | | x > 1 | ||
LL | | } | ||
LL | | ).unwrap_or_default(); | ||
| |_________________________^ | ||
| | ||
help: use | ||
| | ||
LL ~ let _ = res.is_ok_and(|x| { | ||
LL + x > 1 | ||
LL ~ }); | ||
| | ||
|
||
error: called `map(<f>).unwrap_or_default()` on a `Result` value | ||
--> $DIR/manual_is_variant_and.rs:43:17 | ||
| | ||
LL | let _ = res.map(|x| x > 1) | ||
| _________________^ | ||
LL | | .unwrap_or_default(); | ||
| |____________________________^ help: use: `is_ok_and(|x| x > 1)` | ||
|
||
error: called `map(<f>).unwrap_or_default()` on a `Result` value | ||
--> $DIR/manual_is_variant_and.rs:50:18 | ||
| | ||
LL | let _ = res2.map(char::is_alphanumeric).unwrap_or_default(); // should lint | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `is_ok_and(char::is_alphanumeric)` | ||
|
||
error: aborting due to 8 previous errors | ||
|