Skip to content

Commit

Permalink
add lint less_concise_than_option_unwrap_or
Browse files Browse the repository at this point in the history
  • Loading branch information
tnielens committed Oct 14, 2020
1 parent 0b77c35 commit 07b2da8
Show file tree
Hide file tree
Showing 12 changed files with 345 additions and 76 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -1781,6 +1781,7 @@ Released 2018-09-13
[`large_stack_arrays`]: https://rust-lang.github.io/rust-clippy/master/index.html#large_stack_arrays
[`len_without_is_empty`]: https://rust-lang.github.io/rust-clippy/master/index.html#len_without_is_empty
[`len_zero`]: https://rust-lang.github.io/rust-clippy/master/index.html#len_zero
[`less_concise_than_option_unwrap_or`]: https://rust-lang.github.io/rust-clippy/master/index.html#less_concise_than_option_unwrap_or
[`let_and_return`]: https://rust-lang.github.io/rust-clippy/master/index.html#let_and_return
[`let_underscore_lock`]: https://rust-lang.github.io/rust-clippy/master/index.html#let_underscore_lock
[`let_underscore_must_use`]: https://rust-lang.github.io/rust-clippy/master/index.html#let_underscore_must_use
Expand Down
107 changes: 107 additions & 0 deletions clippy_lints/src/less_concise_than.rs
@@ -0,0 +1,107 @@
use crate::utils;
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::{def, Arm, Expr, ExprKind, PatKind, QPath};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};

declare_clippy_lint! {
/// **What it does:**
/// Finds patterns that can be encoded more concisely with `Option::unwrap_or`.
///
/// **Why is this bad?**
/// Concise code helps focusing on behavior instead of boilerplate.
///
/// **Known problems:** None.
///
/// **Example:**
/// ```rust
/// match int_optional {
/// Some(v) => v,
/// None => 1,
/// }
/// ```
///
/// Use instead:
/// ```rust
/// int_optional.unwrap_or(1)
/// ```
pub LESS_CONCISE_THAN_OPTION_UNWRAP_OR,
pedantic,
"finds patterns that can be encoded more concisely with `Option::unwrap_or`"
}

declare_lint_pass!(LessConciseThan => [LESS_CONCISE_THAN_OPTION_UNWRAP_OR]);

impl LateLintPass<'_> for LessConciseThan {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
if utils::in_macro(expr.span) {
return;
}
if lint_option_unwrap_or_case(cx, expr) {
return;
}
}
}

fn lint_option_unwrap_or_case<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> bool {
#[allow(clippy::needless_bool)]
fn applicable_none_arm<'a>(arms: &'a [Arm<'a>]) -> Option<&'a Arm<'a>> {
if_chain! {
if arms.len() == 2;
if arms.iter().all(|arm| arm.guard.is_none());
if let Some((idx, none_arm)) = arms.iter().enumerate().find(|(_, arm)|
if_chain! {
if let PatKind::Path(ref qpath) = arm.pat.kind;
if utils::match_qpath(qpath, &utils::paths::OPTION_NONE);
then { true }
else { false }
}
);
let some_arm = &arms[1 - idx];
if let PatKind::TupleStruct(ref some_qpath, &[some_binding], _) = some_arm.pat.kind;
if utils::match_qpath(some_qpath, &utils::paths::OPTION_SOME);
if let PatKind::Binding(_, binding_hir_id, ..) = some_binding.kind;
if let ExprKind::Path(QPath::Resolved(_, body_path)) = some_arm.body.kind;
if let def::Res::Local(body_path_hir_id) = body_path.res;
if body_path_hir_id == binding_hir_id;
then { Some(none_arm) }
else { None }
}
}
if_chain! {
if !utils::usage::contains_return_break_continue_macro(expr);
if let ExprKind::Match (match_expr, match_arms, _) = expr.kind;
let ty = cx.typeck_results().expr_ty(match_expr);
if utils::is_type_diagnostic_item(cx, ty, sym!(option_type));
if let Some(none_arm) = applicable_none_arm(match_arms);
if let Some(match_expr_snippet) = utils::snippet_opt(cx, match_expr.span);
if let Some(none_body_snippet) = utils::snippet_opt(cx, none_arm.body.span);
if let Some(indent) = utils::indent_of(cx, expr.span);
then {
let reindented_none_body =
utils::reindent_multiline(none_body_snippet.into(), true, Some(indent));
let eager_eval = utils::eager_or_lazy::is_eagerness_candidate(cx, none_arm.body);
let method = if eager_eval {
"unwrap_or"
} else {
"unwrap_or_else"
};
utils::span_lint_and_sugg(
cx,
LESS_CONCISE_THAN_OPTION_UNWRAP_OR, expr.span,
"this pattern can be more concisely encoded with `Option::unwrap_or`",
"replace with",
format!(
"{}.{}({}{})",
match_expr_snippet,
method,
if eager_eval { ""} else { "|| " },
reindented_none_body
),
Applicability::MachineApplicable,
);
true
} else { false}
}
}
4 changes: 4 additions & 0 deletions clippy_lints/src/lib.rs
Expand Up @@ -224,6 +224,7 @@ mod large_const_arrays;
mod large_enum_variant;
mod large_stack_arrays;
mod len_zero;
mod less_concise_than;
mod let_if_seq;
mod let_underscore;
mod lifetimes;
Expand Down Expand Up @@ -609,6 +610,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
&large_stack_arrays::LARGE_STACK_ARRAYS,
&len_zero::LEN_WITHOUT_IS_EMPTY,
&len_zero::LEN_ZERO,
&less_concise_than::LESS_CONCISE_THAN_OPTION_UNWRAP_OR,
&let_if_seq::USELESS_LET_IF_SEQ,
&let_underscore::LET_UNDERSCORE_LOCK,
&let_underscore::LET_UNDERSCORE_MUST_USE,
Expand Down Expand Up @@ -1126,6 +1128,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
store.register_late_pass(|| box repeat_once::RepeatOnce);
store.register_late_pass(|| box unwrap_in_result::UnwrapInResult);
store.register_late_pass(|| box self_assignment::SelfAssignment);
store.register_late_pass(|| box less_concise_than::LessConciseThan);
store.register_late_pass(|| box float_equality_without_abs::FloatEqualityWithoutAbs);
store.register_late_pass(|| box async_yields_async::AsyncYieldsAsync);
store.register_late_pass(|| box manual_strip::ManualStrip);
Expand Down Expand Up @@ -1210,6 +1213,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
LintId::of(&infinite_iter::MAYBE_INFINITE_ITER),
LintId::of(&items_after_statements::ITEMS_AFTER_STATEMENTS),
LintId::of(&large_stack_arrays::LARGE_STACK_ARRAYS),
LintId::of(&less_concise_than::LESS_CONCISE_THAN_OPTION_UNWRAP_OR),
LintId::of(&literal_representation::LARGE_DIGIT_GROUPS),
LintId::of(&literal_representation::UNREADABLE_LITERAL),
LintId::of(&loops::EXPLICIT_INTO_ITER_LOOP),
Expand Down
53 changes: 2 additions & 51 deletions clippy_lints/src/option_if_let_else.rs
Expand Up @@ -5,10 +5,8 @@ use crate::utils::{is_type_diagnostic_item, paths, span_lint_and_sugg};
use if_chain::if_chain;

use rustc_errors::Applicability;
use rustc_hir::intravisit::{NestedVisitorMap, Visitor};
use rustc_hir::{Arm, BindingAnnotation, Block, Expr, ExprKind, MatchSource, Mutability, PatKind, UnOp};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::hir::map::Map;
use rustc_session::{declare_lint_pass, declare_tool_lint};

declare_clippy_lint! {
Expand Down Expand Up @@ -84,53 +82,6 @@ struct OptionIfLetElseOccurence {
wrap_braces: bool,
}

struct ReturnBreakContinueMacroVisitor {
seen_return_break_continue: bool,
}

impl ReturnBreakContinueMacroVisitor {
fn new() -> ReturnBreakContinueMacroVisitor {
ReturnBreakContinueMacroVisitor {
seen_return_break_continue: false,
}
}
}

impl<'tcx> Visitor<'tcx> for ReturnBreakContinueMacroVisitor {
type Map = Map<'tcx>;
fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
NestedVisitorMap::None
}

fn visit_expr(&mut self, ex: &'tcx Expr<'tcx>) {
if self.seen_return_break_continue {
// No need to look farther if we've already seen one of them
return;
}
match &ex.kind {
ExprKind::Ret(..) | ExprKind::Break(..) | ExprKind::Continue(..) => {
self.seen_return_break_continue = true;
},
// Something special could be done here to handle while or for loop
// desugaring, as this will detect a break if there's a while loop
// or a for loop inside the expression.
_ => {
if utils::in_macro(ex.span) {
self.seen_return_break_continue = true;
} else {
rustc_hir::intravisit::walk_expr(self, ex);
}
},
}
}
}

fn contains_return_break_continue_macro(expression: &Expr<'_>) -> bool {
let mut recursive_visitor = ReturnBreakContinueMacroVisitor::new();
recursive_visitor.visit_expr(expression);
recursive_visitor.seen_return_break_continue
}

/// Extracts the body of a given arm. If the arm contains only an expression,
/// then it returns the expression. Otherwise, it returns the entire block
fn extract_body_from_arm<'a>(arm: &'a Arm<'a>) -> Option<&'a Expr<'a>> {
Expand Down Expand Up @@ -208,8 +159,8 @@ fn detect_option_if_let_else<'tcx>(
if let PatKind::TupleStruct(struct_qpath, &[inner_pat], _) = &arms[0].pat.kind;
if utils::match_qpath(struct_qpath, &paths::OPTION_SOME);
if let PatKind::Binding(bind_annotation, _, id, _) = &inner_pat.kind;
if !contains_return_break_continue_macro(arms[0].body);
if !contains_return_break_continue_macro(arms[1].body);
if !utils::usage::contains_return_break_continue_macro(arms[0].body);
if !utils::usage::contains_return_break_continue_macro(arms[1].body);
then {
let capture_mut = if bind_annotation == &BindingAnnotation::Mutable { "mut " } else { "" };
let some_body = extract_body_from_arm(&arms[0])?;
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/utils/eager_or_lazy.rs
Expand Up @@ -82,7 +82,7 @@ fn identify_some_pure_patterns(expr: &Expr<'_>) -> bool {
/// Identify some potentially computationally expensive patterns.
/// This function is named so to stress that its implementation is non-exhaustive.
/// It returns FNs and FPs.
fn identify_some_potentially_expensive_patterns<'a, 'tcx>(cx: &'a LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> bool {
fn identify_some_potentially_expensive_patterns<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> bool {
// Searches an expression for method calls or function calls that aren't ctors
struct FunCallFinder<'a, 'tcx> {
cx: &'a LateContext<'tcx>,
Expand Down
50 changes: 49 additions & 1 deletion clippy_lints/src/utils/usage.rs
@@ -1,10 +1,11 @@
use crate::utils;
use crate::utils::match_var;
use rustc_data_structures::fx::FxHashSet;
use rustc_hir as hir;
use rustc_hir::def::Res;
use rustc_hir::intravisit;
use rustc_hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
use rustc_hir::{Expr, HirId, Path};
use rustc_hir::{Expr, ExprKind, HirId, Path};
use rustc_infer::infer::TyCtxtInferExt;
use rustc_lint::LateContext;
use rustc_middle::hir::map::Map;
Expand Down Expand Up @@ -174,3 +175,50 @@ impl<'a, 'tcx> intravisit::Visitor<'tcx> for BindingUsageFinder<'a, 'tcx> {
intravisit::NestedVisitorMap::OnlyBodies(self.cx.tcx.hir())
}
}

struct ReturnBreakContinueMacroVisitor {
seen_return_break_continue: bool,
}

impl ReturnBreakContinueMacroVisitor {
fn new() -> ReturnBreakContinueMacroVisitor {
ReturnBreakContinueMacroVisitor {
seen_return_break_continue: false,
}
}
}

impl<'tcx> Visitor<'tcx> for ReturnBreakContinueMacroVisitor {
type Map = Map<'tcx>;
fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
NestedVisitorMap::None
}

fn visit_expr(&mut self, ex: &'tcx Expr<'tcx>) {
if self.seen_return_break_continue {
// No need to look farther if we've already seen one of them
return;
}
match &ex.kind {
ExprKind::Ret(..) | ExprKind::Break(..) | ExprKind::Continue(..) => {
self.seen_return_break_continue = true;
},
// Something special could be done here to handle while or for loop
// desugaring, as this will detect a break if there's a while loop
// or a for loop inside the expression.
_ => {
if utils::in_macro(ex.span) {
self.seen_return_break_continue = true;
} else {
rustc_hir::intravisit::walk_expr(self, ex);
}
},
}
}
}

pub fn contains_return_break_continue_macro(expression: &Expr<'_>) -> bool {
let mut recursive_visitor = ReturnBreakContinueMacroVisitor::new();
recursive_visitor.visit_expr(expression);
recursive_visitor.seen_return_break_continue
}
7 changes: 7 additions & 0 deletions src/lintlist/mod.rs
Expand Up @@ -1075,6 +1075,13 @@ vec![
deprecation: None,
module: "len_zero",
},
Lint {
name: "less_concise_than_option_unwrap_or",
group: "pedantic",
desc: "finds patterns that can be encoded more concisely with `Option::unwrap_or`",
deprecation: None,
module: "less_concise_than",
},
Lint {
name: "let_and_return",
group: "style",
Expand Down
43 changes: 43 additions & 0 deletions tests/ui/less_concise_than.fixed
@@ -0,0 +1,43 @@
// run-rustfix
#![warn(clippy::less_concise_than_option_unwrap_or)]
#![allow(dead_code)]

fn unwrap_or() {
// int case
Some(1).unwrap_or(42);

// richer none expr
Some(1).unwrap_or_else(|| 1 + 42);

// multiline case
Some(1).unwrap_or_else(|| {
let a = 1 + 42;
let b = a + 42;
b + 42
});

// string case
Some("Bob").unwrap_or("Alice");

// don't lint
match Some(1) {
Some(i) => i + 2,
None => 42,
};
match Some(1) {
Some(i) => i,
None => return,
};
for j in 0..4 {
match Some(j) {
Some(i) => i,
None => continue,
};
match Some(j) {
Some(i) => i,
None => break,
};
}
}

fn main() {}

0 comments on commit 07b2da8

Please sign in to comment.