-
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 #7101 - camsteffen:flat-map-option, r=giraffate
Add flat_map_option lint changelog: Add flat_map_option lint Closes #2241
- Loading branch information
Showing
9 changed files
with
110 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::is_trait_method; | ||
use rustc_errors::Applicability; | ||
use rustc_hir as hir; | ||
use rustc_lint::LateContext; | ||
use rustc_middle::ty; | ||
use rustc_span::{source_map::Span, sym}; | ||
|
||
use super::FLAT_MAP_OPTION; | ||
use clippy_utils::ty::is_type_diagnostic_item; | ||
|
||
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, arg: &'tcx hir::Expr<'_>, span: Span) { | ||
if !is_trait_method(cx, expr, sym::Iterator) { | ||
return; | ||
} | ||
let arg_ty = cx.typeck_results().expr_ty_adjusted(arg); | ||
let sig = match arg_ty.kind() { | ||
ty::Closure(_, substs) => substs.as_closure().sig(), | ||
_ if arg_ty.is_fn() => arg_ty.fn_sig(cx.tcx), | ||
_ => return, | ||
}; | ||
if !is_type_diagnostic_item(cx, sig.output().skip_binder(), sym::option_type) { | ||
return; | ||
} | ||
span_lint_and_sugg( | ||
cx, | ||
FLAT_MAP_OPTION, | ||
span, | ||
"used `flat_map` where `filter_map` could be used instead", | ||
"try", | ||
"filter_map".into(), | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// run-rustfix | ||
#![warn(clippy::flat_map_option)] | ||
#![allow(clippy::redundant_closure, clippy::unnecessary_filter_map)] | ||
|
||
fn main() { | ||
// yay | ||
let c = |x| Some(x); | ||
let _ = [1].iter().filter_map(c); | ||
let _ = [1].iter().filter_map(Some); | ||
|
||
// nay | ||
let _ = [1].iter().flat_map(|_| &Some(1)); | ||
} |
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,13 @@ | ||
// run-rustfix | ||
#![warn(clippy::flat_map_option)] | ||
#![allow(clippy::redundant_closure, clippy::unnecessary_filter_map)] | ||
|
||
fn main() { | ||
// yay | ||
let c = |x| Some(x); | ||
let _ = [1].iter().flat_map(c); | ||
let _ = [1].iter().flat_map(Some); | ||
|
||
// nay | ||
let _ = [1].iter().flat_map(|_| &Some(1)); | ||
} |
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,16 @@ | ||
error: used `flat_map` where `filter_map` could be used instead | ||
--> $DIR/flat_map_option.rs:8:24 | ||
| | ||
LL | let _ = [1].iter().flat_map(c); | ||
| ^^^^^^^^ help: try: `filter_map` | ||
| | ||
= note: `-D clippy::flat-map-option` implied by `-D warnings` | ||
|
||
error: used `flat_map` where `filter_map` could be used instead | ||
--> $DIR/flat_map_option.rs:9:24 | ||
| | ||
LL | let _ = [1].iter().flat_map(Some); | ||
| ^^^^^^^^ help: try: `filter_map` | ||
|
||
error: aborting due to 2 previous errors | ||
|