Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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 @@ -5164,6 +5164,7 @@ Released 2018-09-13
[`collection_is_never_read`]: https://rust-lang.github.io/rust-clippy/master/index.html#collection_is_never_read
[`comparison_chain`]: https://rust-lang.github.io/rust-clippy/master/index.html#comparison_chain
[`comparison_to_empty`]: https://rust-lang.github.io/rust-clippy/master/index.html#comparison_to_empty
[`compressable_if`]: https://rust-lang.github.io/rust-clippy/master/index.html#compressable_if
[`const_is_empty`]: https://rust-lang.github.io/rust-clippy/master/index.html#const_is_empty
[`const_static_lifetime`]: https://rust-lang.github.io/rust-clippy/master/index.html#const_static_lifetime
[`copy_iterator`]: https://rust-lang.github.io/rust-clippy/master/index.html#copy_iterator
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/attrs/allow_attributes_without_reason.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::compressable_if)]
use super::{Attribute, ALLOW_ATTRIBUTES_WITHOUT_REASON};
use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::is_from_proc_macro;
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/bool_assert_comparison.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::compressable_if)]
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm a bit wary to add a complexity lint that needs multiple allow annotations in our own code.

I'd either see the lint applied everywhere we can (which would show if it actually is an improvement), or keep it in nursery or pedantic for now.

The last thing we need is a lint that will trigger a lot on average code with a warning that has no urgency at all.

use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::macros::{find_assert_eq_args, root_macro_call_first_node};
use clippy_utils::sugg::Sugg;
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/casts/cast_possible_wrap.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::compressable_if)]
use clippy_utils::diagnostics::span_lint_and_then;
use rustc_hir::Expr;
use rustc_lint::LateContext;
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/casts/cast_slice_different_sizes.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::compressable_if)]
use clippy_config::msrvs::{self, Msrv};
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::source;
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/comparison_chain.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::compressable_if)]
use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::ty::implements_trait;
use clippy_utils::{if_sequence, in_constant, is_else_clause, SpanlessEq};
Expand Down
176 changes: 176 additions & 0 deletions clippy_lints/src/compressable_if.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
use clippy_utils::diagnostics::{multispan_sugg_with_applicability, span_lint_and_then};
use clippy_utils::source::snippet;
use clippy_utils::SpanlessEq;
use rustc_errors::MultiSpan;
use rustc_hir::def_id::LocalDefId;
use rustc_hir::intravisit::FnKind;
use rustc_hir::{Body, Expr, ExprKind, FnDecl, StmtKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::declare_lint_pass;
use rustc_span::Span;

declare_clippy_lint! {
/// ### What it does
/// Checks for two `if` blocks with the same content
/// and suggests merging them.
/// ### Why is this bad?
/// This make the code more complex and harder to read.
/// ### Example
/// ```no_run
/// let a = 1;
/// if a == 1 {
/// println!("odd");
/// }
/// if a == 3 {
/// println!("odd");
/// }
/// ```
/// Use instead:
/// ```no_run
/// let a = 1;
/// if a == 1 || a == 3 {
/// println!("odd");
/// }
/// ```
#[clippy::version = "1.79.0"]
pub COMPRESSABLE_IF,
complexity,
"if two `if` blocks have the same content, they can be merged"
}

declare_lint_pass!(CompressableIf => [COMPRESSABLE_IF]);

impl<'tcx> LateLintPass<'tcx> for CompressableIf {
fn check_fn(
&mut self,
cx: &LateContext<'tcx>,
_: FnKind<'tcx>,
_: &'tcx FnDecl<'_>,
body: &'tcx Body<'_>,
_fn_span: Span,
_: LocalDefId,
) {
// DEBUG
// println!("\n\n\n");

let mut compressables = function_has_compressable_if(cx, body);
while let Some((first, second)) = compressables.pop() {
// span_lint_and_note(
// cx,
// COMPRESSABLE_IF,
// fn_span,
// "these if blocks have the same content and no side effects",
// None,
// "consider merging them into a single if block with a combined condition",
// );

// Construct multi-span lint
let spans = MultiSpan::from_spans(vec![first.span, second.span]);

// Deconstruct the pair into cond and then
let ExprKind::If(first_cond, first_then, _) = first.kind else {
unreachable!()
};

let ExprKind::If(second_cond, _, _) = second.kind else {
unreachable!()
};

span_lint_and_then(
cx,
COMPRESSABLE_IF,
spans,
"these if blocks have the same content and no side effects",
|diag| {
diag.help("consider merging them into a single if block with a combined condition");
// diag.span_suggestion(
// first_cond.span,
// "merge these if blocks",
// format!("if {} || {}", snippet(cx, first_cond.span, ".."), snippet(cx, second_cond.span,
// "..")), rustc_errors::Applicability::MaybeIncorrect,
// );
// diag.span_suggestion(
// second.span,
// "remove this if block",
// String::new(),
// rustc_errors::Applicability::MaybeIncorrect,
// );
// Multispan instead???
multispan_sugg_with_applicability(
diag,
"merge these if blocks",
rustc_errors::Applicability::Unspecified,
// vec<(span, replace)>
vec![(
first.span,
format!(
"if {} || {} {} ",
snippet(cx, first_cond.span, ".."),
snippet(cx, second_cond.span, ".."),
snippet(cx, first_then.span, "..")
),
)]
.into_iter()
.chain(vec![(second.span, String::new())]),
);
},
);
}
}
}

fn function_has_compressable_if<'b>(cx: &LateContext<'_>, body: &Body<'b>) -> Vec<(&'b Expr<'b>, &'b Expr<'b>)> {
// The body is an Expr, how is it structured?
// It's a block, which contains a list of statements.

let to_debug = body.value.kind;
if let ExprKind::Block(to_debug, _) = to_debug {
let statements = to_debug.stmts; // Does not include the final expression

// DEBUG
// for statement in statements.iter() {
// println!("{:?}\n", statement);
// }

// Now we have a list of statements.
// We want to find a pair of if statements that
// 1. Have the same content (In progress)
// 2. Have no side effects between them (Not implemented)

let mut found_if: Vec<&Expr<'_>> = Vec::new(); // The list of found if statements
let mut found_compress: Vec<(&Expr<'_>, &Expr<'_>)> = Vec::new(); // The list of found compressable if statements

let mut spannless_eq = SpanlessEq::new(cx); // for comparing expressions

for stmt in statements {
if let StmtKind::Expr(expr) = stmt.kind {
if let ExprKind::If(_, _, _) = expr.kind {
// before pushing, check if the then is the same as any of the previous thens

for prev in &found_if {
if let ExprKind::If(_, prev_then, _) = prev.kind
&& let ExprKind::If(_, expr_then, _) = expr.kind
&& spannless_eq.eq_expr(expr_then, prev_then)
{
// The thens are the same, we have a compressable if
// found_compress.push((prev_cond, then));
found_compress.push((prev, expr));
}
}

found_if.push(expr);
} else {
// If the next expression is not an if, we can't be sure that the ifs can be merged
found_if.clear();
}
} else {
// If the next statement is not an expression, it can't be an if, so the same thing applies
found_if.clear();
}
}

return found_compress;
}

Vec::new()
}
1 change: 1 addition & 0 deletions clippy_lints/src/copies.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::compressable_if)]
use clippy_utils::diagnostics::{span_lint_and_note, span_lint_and_then};
use clippy_utils::source::{first_line_of_span, indent_of, reindent_multiline, snippet, snippet_opt};
use clippy_utils::ty::{is_interior_mut_ty, needs_ordered_drop};
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/declared_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
crate::collapsible_if::COLLAPSIBLE_IF_INFO,
crate::collection_is_never_read::COLLECTION_IS_NEVER_READ_INFO,
crate::comparison_chain::COMPARISON_CHAIN_INFO,
crate::compressable_if::COMPRESSABLE_IF_INFO,
crate::copies::BRANCHES_SHARING_CODE_INFO,
crate::copies::IFS_SAME_COND_INFO,
crate::copies::IF_SAME_THEN_ELSE_INFO,
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/derive.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::compressable_if)]
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_note, span_lint_and_sugg, span_lint_and_then};
use clippy_utils::ty::{implements_trait, implements_trait_with_env, is_copy};
use clippy_utils::{has_non_exhaustive_attr, is_lint_allowed, match_def_path, paths};
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/doc/missing_headers.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::compressable_if)]
use clippy_utils::diagnostics::{span_lint, span_lint_and_note};
use clippy_utils::ty::{implements_trait, is_type_diagnostic_item};
use clippy_utils::{is_doc_hidden, return_ty};
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/endian_bytes.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::compressable_if)]
use crate::Lint;
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::is_lint_allowed;
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/format_args.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::compressable_if)]
use arrayvec::ArrayVec;
use clippy_config::msrvs::{self, Msrv};
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/from_over_into.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::compressable_if)]
use clippy_config::msrvs::{self, Msrv};
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::macros::span_is_local;
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/if_then_some_else_none.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::compressable_if)]
use clippy_config::msrvs::{self, Msrv};
use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::eager_or_lazy::switch_to_eager_eval;
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/incompatible_msrv.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::compressable_if)]
use clippy_config::msrvs::Msrv;
use clippy_utils::diagnostics::span_lint;
use clippy_utils::is_in_test_function;
Expand Down
2 changes: 2 additions & 0 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ mod cognitive_complexity;
mod collapsible_if;
mod collection_is_never_read;
mod comparison_chain;
mod compressable_if;
mod copies;
mod copy_iterator;
mod crate_in_macro_def;
Expand Down Expand Up @@ -1131,6 +1132,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
store.register_late_pass(|_| Box::new(zero_repeat_side_effects::ZeroRepeatSideEffects));
store.register_late_pass(|_| Box::new(manual_unwrap_or_default::ManualUnwrapOrDefault));
store.register_late_pass(|_| Box::new(integer_division_remainder_used::IntegerDivisionRemainderUsed));
store.register_late_pass(|_| Box::new(compressable_if::CompressableIf));
// add lints here, do not remove this comment, it's used in `new_lint`
}

Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/literal_representation.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::compressable_if)]
//! Lints concerned with the grouping of digits with underscores in integral or
//! floating-point literal expressions.

Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/loops/infinite_loop.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::compressable_if)]
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::{fn_def_id, is_from_proc_macro, is_lint_allowed};
use hir::intravisit::{walk_expr, Visitor};
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/loops/needless_range_loop.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::compressable_if)]
use super::NEEDLESS_RANGE_LOOP;
use clippy_utils::diagnostics::{multispan_sugg, span_lint_and_then};
use clippy_utils::source::snippet;
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/loops/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::compressable_if)]
use clippy_utils::ty::{has_iter_method, implements_trait};
use clippy_utils::{get_parent_expr, is_integer_const, path_to_local, path_to_local_id, sugg};
use rustc_ast::ast::{LitIntType, LitKind};
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/manual_clamp.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::compressable_if)]
use clippy_config::msrvs::{self, Msrv};
use clippy_utils::consts::{constant, Constant};
use clippy_utils::diagnostics::{span_lint_and_then, span_lint_hir_and_then};
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/manual_is_ascii_check.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::compressable_if)]
use clippy_config::msrvs::{self, Msrv};
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::macros::root_macro_call;
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/manual_rem_euclid.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::compressable_if)]
use clippy_config::msrvs::{self, Msrv};
use clippy_utils::consts::{constant_full_int, FullInt};
use clippy_utils::diagnostics::span_lint_and_sugg;
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/matches/manual_utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::compressable_if)]
use crate::map_unit_fn::OPTION_MAP_UNIT_FN;
use crate::matches::MATCH_AS_REF;
use clippy_utils::source::{snippet_with_applicability, snippet_with_context};
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/methods/inefficient_to_string.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::compressable_if)]
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::ty::{is_type_lang_item, walk_ptrs_ty_depth};
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/methods/is_digit_ascii_radix.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::compressable_if)]
//! Lint for `c.is_digit(10)`

use super::IS_DIGIT_ASCII_RADIX;
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/methods/manual_is_variant_and.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::compressable_if)]
use clippy_config::msrvs::{Msrv, OPTION_RESULT_IS_VARIANT_AND};
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet;
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/methods/option_map_or_none.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::compressable_if)]
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet;
use clippy_utils::ty::is_type_diagnostic_item;
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/methods/unnecessary_fold.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::compressable_if)]
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::{is_trait_method, path_to_local_id, peel_blocks, strip_pat_refs};
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/misc.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::compressable_if)]
use clippy_utils::diagnostics::{span_lint, span_lint_and_then, span_lint_hir_and_then};
use clippy_utils::source::{snippet, snippet_with_context};
use clippy_utils::sugg::Sugg;
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/missing_const_for_fn.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::compressable_if)]
use clippy_config::msrvs::{self, Msrv};
use clippy_utils::diagnostics::span_lint;
use clippy_utils::qualify_min_const_fn::is_min_const_fn;
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/missing_doc.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::compressable_if)]
// Note: More specifically this lint is largely inspired (aka copied) from
// *rustc*'s
// [`missing_doc`].
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/missing_inline.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::compressable_if)]
use clippy_utils::diagnostics::span_lint;
use rustc_ast::ast;
use rustc_hir as hir;
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/needless_borrows_for_generic_args.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::compressable_if)]
use clippy_config::msrvs::{self, Msrv};
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::mir::{enclosing_mir, expr_local, local_assignments, used_exactly_once, PossibleBorrowerMap};
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/needless_pass_by_ref_mut.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::compressable_if)]
use super::needless_pass_by_value::requires_exact_signature;
use clippy_utils::diagnostics::span_lint_hir_and_then;
use clippy_utils::source::snippet;
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/non_expressive_names.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::compressable_if)]
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
use rustc_ast::ast::{
self, Arm, AssocItem, AssocItemKind, Attribute, Block, FnDecl, Item, ItemKind, Local, Pat, PatKind,
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/non_send_fields_in_send_ty.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::compressable_if)]
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::is_lint_allowed;
use clippy_utils::source::snippet;
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/operators/absurd_extreme_comparisons.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::compressable_if)]
use rustc_hir::{BinOpKind, Expr, ExprKind};
use rustc_lint::LateContext;
use rustc_middle::ty;
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/operators/arithmetic_side_effects.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::compressable_if)]
use super::ARITHMETIC_SIDE_EFFECTS;
use clippy_utils::consts::{constant, constant_simple, Constant};
use clippy_utils::diagnostics::span_lint;
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/redundant_field_names.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::compressable_if)]
use clippy_config::msrvs::{self, Msrv};
use clippy_utils::diagnostics::span_lint_and_sugg;
use rustc_ast::ast::{Expr, ExprKind};
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/shadow.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::compressable_if)]
use clippy_utils::diagnostics::span_lint_and_note;
use clippy_utils::source::snippet;
use clippy_utils::visitors::is_local_used;
Expand Down
Loading