Skip to content
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

Implement new lint: if_then_some_else_none #6859

Merged
merged 4 commits into from
Mar 13, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2103,6 +2103,7 @@ Released 2018-09-13
[`if_let_some_result`]: https://rust-lang.github.io/rust-clippy/master/index.html#if_let_some_result
[`if_not_else`]: https://rust-lang.github.io/rust-clippy/master/index.html#if_not_else
[`if_same_then_else`]: https://rust-lang.github.io/rust-clippy/master/index.html#if_same_then_else
[`if_then_some_else_none`]: https://rust-lang.github.io/rust-clippy/master/index.html#if_then_some_else_none
[`ifs_same_cond`]: https://rust-lang.github.io/rust-clippy/master/index.html#ifs_same_cond
[`implicit_clone`]: https://rust-lang.github.io/rust-clippy/master/index.html#implicit_clone
[`implicit_hasher`]: https://rust-lang.github.io/rust-clippy/master/index.html#implicit_hasher
Expand Down
105 changes: 105 additions & 0 deletions clippy_lints/src/if_then_some_else_none.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
use crate::utils;
use if_chain::if_chain;
use rustc_hir::{Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::lint::in_external_macro;
use rustc_semver::RustcVersion;
use rustc_session::{declare_tool_lint, impl_lint_pass};

const IF_THEN_SOME_ELSE_NONE_MSRV: RustcVersion = RustcVersion::new(1, 50, 0);

declare_clippy_lint! {
/// **What it does:** Checks for if-else that could be written to `bool::then`.
///
/// **Why is this bad?** Looks a little redundant. Using `bool::then` helps it have less lines of code.
///
/// **Known problems:** None.
///
/// **Example:**
///
/// ```rust
/// # let v = vec![0];
/// let a = if v.is_empty() {
/// println!("true!");
/// Some(42)
/// } else {
/// None
/// };
/// ```
///
/// Could be written:
///
/// ```rust
/// # let v = vec![0];
/// let a = v.is_empty().then(|| {
/// println!("true!");
/// 42
/// });
/// ```
pub IF_THEN_SOME_ELSE_NONE,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the MSRV feature is pretty new an we don't have any automated checks for it yet:

If an MSRV is added to a lint, the lint has also to be added in the config option. I wrote documentation about this in #6977. cc @rust-lang/clippy

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will prepare a fix for this (and possible other lints soon.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If an MSRV is added to a lint, the lint has also to be added in the config option.

Oh I didn't know that. I'll keep that in mind for the next time I implement a new lint.

I will prepare a fix for this (and possible other lints soon.

Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your follow-up!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I didn't know that. I'll keep that in mind for the next time I implement a new lint.

No worries, this isn't/wasn't documented anywhere, so that's on me for missing it while reviewing the documentation.

restriction,
"Finds if-else that could be written using `bool::then`"
}

pub struct IfThenSomeElseNone {
msrv: Option<RustcVersion>,
}

impl IfThenSomeElseNone {
#[must_use]
pub fn new(msrv: Option<RustcVersion>) -> Self {
Self { msrv }
}
}

impl_lint_pass!(IfThenSomeElseNone => [IF_THEN_SOME_ELSE_NONE]);

impl LateLintPass<'_> for IfThenSomeElseNone {
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'tcx Expr<'_>) {
if !utils::meets_msrv(self.msrv.as_ref(), &IF_THEN_SOME_ELSE_NONE_MSRV) {
return;
}

if in_external_macro(cx.sess(), expr.span) {
return;
}

// We only care about the top-most `if` in the chain
if utils::parent_node_is_if_expr(expr, cx) {
return;
}

if_chain! {
if let ExprKind::If(ref cond, ref then, Some(ref els)) = expr.kind;
if let ExprKind::Block(ref then_block, _) = then.kind;
if let Some(ref then_expr) = then_block.expr;
if let ExprKind::Call(ref then_call, [then_arg]) = then_expr.kind;
if let ExprKind::Path(ref then_call_qpath) = then_call.kind;
if utils::match_qpath(then_call_qpath, &utils::paths::OPTION_SOME);
if let ExprKind::Block(ref els_block, _) = els.kind;
if els_block.stmts.is_empty();
if let Some(ref els_expr) = els_block.expr;
if let ExprKind::Path(ref els_call_qpath) = els_expr.kind;
if utils::match_qpath(els_call_qpath, &utils::paths::OPTION_NONE);
then {
let cond_snip = utils::snippet(cx, cond.span, "[condition]");
let arg_snip = utils::snippet(cx, then_arg.span, "");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can test codes like below be added? Using macro, the suggested code may be broken, so it would be better to use the method like snippet_with_macro_callsite.

    let _ = if matches!(true, true) {
        println!("true!");
        Some(matches!(true, false))
    } else {
        None
    };

Additionally, can test codes like below be added? The parenthesis isn't considered, so the suggested codes seem not to work.

    let x = Some(5);
    let _ = x.and_then(|o| if o < 32 { Some(o) } else { None });

let help = format!(
"consider using `bool::then` like: `{}.then(|| {{ /* snippet */ {} }})`",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If then block has no statements, it would be better to remove the placeholder like /* snippet */.

cond_snip,
arg_snip,
);
utils::span_lint_and_help(
cx,
IF_THEN_SOME_ELSE_NONE,
expr.span,
"this could be simplified with `bool::then`",
None,
&help,
);
}
}
}

extract_msrv_attr!(LateContext);
}
4 changes: 4 additions & 0 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ mod identity_op;
mod if_let_mutex;
mod if_let_some_result;
mod if_not_else;
mod if_then_some_else_none;
mod implicit_return;
mod implicit_saturating_sub;
mod inconsistent_struct_constructor;
Expand Down Expand Up @@ -667,6 +668,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
&if_let_mutex::IF_LET_MUTEX,
&if_let_some_result::IF_LET_SOME_RESULT,
&if_not_else::IF_NOT_ELSE,
&if_then_some_else_none::IF_THEN_SOME_ELSE_NONE,
&implicit_return::IMPLICIT_RETURN,
&implicit_saturating_sub::IMPLICIT_SATURATING_SUB,
&inconsistent_struct_constructor::INCONSISTENT_STRUCT_CONSTRUCTOR,
Expand Down Expand Up @@ -1282,6 +1284,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
store.register_late_pass(|| box redundant_slicing::RedundantSlicing);
store.register_late_pass(|| box from_str_radix_10::FromStrRadix10);
store.register_late_pass(|| box manual_map::ManualMap);
store.register_late_pass(move || box if_then_some_else_none::IfThenSomeElseNone::new(msrv));

store.register_group(true, "clippy::restriction", Some("clippy_restriction"), vec![
LintId::of(&arithmetic::FLOAT_ARITHMETIC),
Expand All @@ -1297,6 +1300,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
LintId::of(&exhaustive_items::EXHAUSTIVE_STRUCTS),
LintId::of(&exit::EXIT),
LintId::of(&float_literal::LOSSY_FLOAT_LITERAL),
LintId::of(&if_then_some_else_none::IF_THEN_SOME_ELSE_NONE),
LintId::of(&implicit_return::IMPLICIT_RETURN),
LintId::of(&indexing_slicing::INDEXING_SLICING),
LintId::of(&inherent_impl::MULTIPLE_INHERENT_IMPL),
Expand Down
88 changes: 88 additions & 0 deletions tests/ui/if_then_some_else_none.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#![warn(clippy::if_then_some_else_none)]
#![feature(custom_inner_attributes)]

fn main() {
// Should issue an error.
let _ = if foo() {
println!("true!");
Some("foo")
} else {
None
};

// Should not issue an error since the `else` block has a statement besides `None`.
let _ = if foo() {
println!("true!");
Some("foo")
} else {
eprintln!("false...");
None
};

// Should not issue an error since there are more than 2 blocks in the if-else chain.
let _ = if foo() {
println!("foo true!");
Some("foo")
} else if bar() {
println!("bar true!");
Some("bar")
} else {
None
};

let _ = if foo() {
println!("foo true!");
Some("foo")
} else {
bar().then(|| {
println!("bar true!");
"bar"
})
};

// Should not issue an error since the `then` block has `None`, not `Some`.
let _ = if foo() { None } else { Some("foo is false") };

// Should not issue an error since the `else` block doesn't use `None` directly.
let _ = if foo() { Some("foo is true") } else { into_none() };

// Should not issue an error since the `then` block doesn't use `Some` directly.
let _ = if foo() { into_some("foo") } else { None };
}

fn _msrv_1_49() {
#![clippy::msrv = "1.49"]
// `bool::then` was stabilized in 1.50. Do not lint this
let _ = if foo() {
println!("true!");
Some(149)
} else {
None
};
}

fn _msrv_1_50() {
#![clippy::msrv = "1.50"]
let _ = if foo() {
println!("true!");
Some(150)
} else {
None
};
}

fn foo() -> bool {
unimplemented!()
}

fn bar() -> bool {
unimplemented!()
}

fn into_some<T>(v: T) -> Option<T> {
Some(v)
}

fn into_none<T>() -> Option<T> {
None
}
31 changes: 31 additions & 0 deletions tests/ui/if_then_some_else_none.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
error: this could be simplified with `bool::then`
--> $DIR/if_then_some_else_none.rs:6:13
|
LL | let _ = if foo() {
| _____________^
LL | | println!("true!");
LL | | Some("foo")
LL | | } else {
LL | | None
LL | | };
| |_____^
|
= note: `-D clippy::if-then-some-else-none` implied by `-D warnings`
= help: consider using `bool::then` like: `foo().then(|| { /* snippet */ "foo" })`

error: this could be simplified with `bool::then`
--> $DIR/if_then_some_else_none.rs:66:13
|
LL | let _ = if foo() {
| _____________^
LL | | println!("true!");
LL | | Some(150)
LL | | } else {
LL | | None
LL | | };
| |_____^
|
= help: consider using `bool::then` like: `foo().then(|| { /* snippet */ 150 })`

error: aborting due to 2 previous errors