-
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.
- Loading branch information
Showing
8 changed files
with
155 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
use crate::utils::paths; | ||
use crate::utils::{in_macro_or_desugar, match_qpath, match_type, remove_blocks, snippet, span_lint_and_sugg}; | ||
use rustc::hir; | ||
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; | ||
use rustc::{declare_lint_pass, declare_tool_lint}; | ||
use rustc_errors::Applicability; | ||
|
||
declare_clippy_lint! { | ||
/// **What it does:** Checks for usage of `_.and_then(|x| Some(y))`. | ||
/// | ||
/// **Why is this bad?** Readability, this can be written more concisely as | ||
/// `_.map(|x| y)`. | ||
/// | ||
/// **Known problems:** None | ||
/// | ||
/// **Example:** | ||
/// | ||
/// ```rust | ||
/// let x = Some("foo"); | ||
/// x.and_then(|s| Some(s.len())); | ||
/// ``` | ||
/// | ||
/// The correct use would be: | ||
/// | ||
/// ```rust | ||
/// let x = Some("foo"); | ||
/// x.map(|s| s.len()); | ||
/// ``` | ||
pub OPTION_AND_THEN_SOME, | ||
complexity, | ||
"using `Option.and_then(|x| Some(y))`, which is more succinctly expressed as `map(|x| y)`" | ||
} | ||
|
||
declare_lint_pass!(AndThenSomeLint => [OPTION_AND_THEN_SOME]); | ||
|
||
const LINT_MSG: &str = "using `Option.and_then(|x| Some(y))`, which is more succinctly expressed as `map(|x| y)`"; | ||
|
||
impl LateLintPass<'_, '_> for AndThenSomeLint { | ||
/// Lint use of `_.and_then(|x| Some(y))` for `Option`s | ||
fn check_expr(&mut self, cx: &LateContext<'_, '_>, e: &hir::Expr) { | ||
if in_macro_or_desugar(e.span) { | ||
return; | ||
} | ||
|
||
if let hir::ExprKind::MethodCall(ref method, _, ref args) = e.node { | ||
if args.len() == 2 && method.ident.as_str() == "and_then" { | ||
let ty = cx.tables.expr_ty(&args[0]); | ||
if !match_type(cx, ty, &paths::OPTION) { | ||
return; | ||
} | ||
match args[1].node { | ||
hir::ExprKind::Closure(_, _, body_id, closure_args_span, _) => { | ||
let closure_body = cx.tcx.hir().body(body_id); | ||
let closure_expr = remove_blocks(&closure_body.value); | ||
|
||
// let note = format!("{:?}", args[1]); | ||
// span_note_and_lint(cx, OPTION_AND_THEN_SOME, closure_args_span, "Outer debugging | ||
// information", closure_args_span, ¬e); | ||
|
||
if let hir::ExprKind::Call(ref some_expr, ref some_args) = closure_expr.node { | ||
if let hir::ExprKind::Path(ref qpath) = some_expr.node { | ||
if match_qpath(qpath, &paths::OPTION_SOME) { | ||
if some_args.len() == 1 { | ||
let inner_expr = &some_args[0]; | ||
let some_inner_snip = snippet(cx, inner_expr.span, ".."); | ||
let closure_args_snip = snippet(cx, closure_args_span, ".."); | ||
let option_snip = snippet(cx, args[0].span, ".."); | ||
let note = | ||
format!("{}.map({} {})", option_snip, closure_args_snip, some_inner_snip); | ||
span_lint_and_sugg( | ||
cx, | ||
OPTION_AND_THEN_SOME, | ||
e.span, | ||
LINT_MSG, | ||
"try this", | ||
note, | ||
Applicability::MachineApplicable, | ||
); | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
// hir::ExprKind::Path(ref qpath) => { | ||
// if match_qpath(qpath, &paths::OPTION_SOME) { | ||
// let note = format!("{:?}", args[1]); | ||
// span_note_and_lint(cx, OPTION_AND_THEN_SOME, args[1].span, "Some debugging information", | ||
// args[1].span, ¬e); } | ||
// }, | ||
_ => {}, | ||
} | ||
} | ||
} | ||
} | ||
} |
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,16 @@ | ||
// run-rustfix | ||
#![deny(clippy::option_and_then_some)] | ||
|
||
// need a main anyway, use it get rid of unused warnings too | ||
pub fn main() { | ||
let x = Some(5); | ||
// the easiest cases | ||
x.and_then(Some); | ||
x.map(|o| o + 1); | ||
// and an easy counter-example | ||
x.and_then(|o| if o < 32 { Some(o) } else { None }); | ||
|
||
// Different type | ||
let x: Result<u32, &str> = Ok(1); | ||
x.and_then(Ok); | ||
} |
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 @@ | ||
// run-rustfix | ||
#![deny(clippy::option_and_then_some)] | ||
|
||
// need a main anyway, use it get rid of unused warnings too | ||
pub fn main() { | ||
let x = Some(5); | ||
// the easiest cases | ||
x.and_then(Some); | ||
x.and_then(|o| Some(o + 1)); | ||
// and an easy counter-example | ||
x.and_then(|o| if o < 32 { Some(o) } else { None }); | ||
|
||
// Different type | ||
let x: Result<u32, &str> = Ok(1); | ||
x.and_then(Ok); | ||
} |
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,14 @@ | ||
error: using `Option.and_then(|x| Some(y))`, which is more succinctly expressed as `map(|x| y)` | ||
--> $DIR/option_and_then_some.rs:9:5 | ||
| | ||
LL | x.and_then(|o| Some(o + 1)); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `x.map(|o| o + 1)` | ||
| | ||
note: lint level defined here | ||
--> $DIR/option_and_then_some.rs:2:9 | ||
| | ||
LL | #![deny(clippy::option_and_then_some)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|