Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 5 additions & 2 deletions clippy_lints/src/collapsible_if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ use clippy_config::Conf;
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
use clippy_utils::msrvs::{self, Msrv};
use clippy_utils::source::{IntoSpan as _, SpanRangeExt, snippet, snippet_block, snippet_block_with_applicability};
use clippy_utils::span_contains_non_comment;
use rustc_ast::BinOpKind;
use rustc_errors::Applicability;
use rustc_hir::{Block, Expr, ExprKind, Stmt, StmtKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::impl_lint_pass;
use rustc_span::Span;
use rustc_span::{BytePos, Span};

declare_clippy_lint! {
/// ### What it does
Expand Down Expand Up @@ -96,6 +97,8 @@ impl CollapsibleIf {
&& cx.tcx.hir_attrs(else_.hir_id).is_empty()
&& !else_.span.from_expansion()
&& let ExprKind::If(..) = else_.kind
&& let up_to_if = else_block.span.until(else_.span)
&& !span_contains_non_comment(cx, up_to_if.with_lo(BytePos(up_to_if.lo().0 + 1)))
{
// Prevent "elseif"
// Check that the "else" is followed by whitespace
Expand Down Expand Up @@ -141,7 +144,7 @@ impl CollapsibleIf {
let then_open_bracket = then.span.split_at(1).0.with_leading_whitespace(cx).into_span();
let then_closing_bracket = {
let end = then.span.shrink_to_hi();
end.with_lo(end.lo() - rustc_span::BytePos(1))
end.with_lo(end.lo() - BytePos(1))
.with_leading_whitespace(cx)
.into_span()
};
Expand Down
12 changes: 11 additions & 1 deletion clippy_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ use rustc_span::hygiene::{ExpnKind, MacroKind};
use rustc_span::source_map::SourceMap;
use rustc_span::symbol::{Ident, Symbol, kw};
use rustc_span::{InnerSpan, Span};
use source::walk_span_to_context;
use source::{SpanRangeExt, walk_span_to_context};
use visitors::{Visitable, for_each_unconsumed_temporary};

use crate::consts::{ConstEvalCtxt, Constant, mir_to_const};
Expand Down Expand Up @@ -2788,6 +2788,16 @@ pub fn span_contains_comment(sm: &SourceMap, span: Span) -> bool {
});
}

/// Checks whether a given span has any non-comment token. This checks for all types of token other
/// than line comment "//", block comment "/**", doc "///" "//!" and whitespace
/// This is useful to determine if there are any actual code tokens in the span that are omitted in
/// the late pass, such as platform-specific code.
pub fn span_contains_non_comment(cx: &impl source::HasSession, span: Span) -> bool {
matches!(span.get_source_text(cx), Some(snippet) if tokenize_with_text(&snippet).any(|(token, _, _)| {
!matches!(token, TokenKind::LineComment { .. } | TokenKind::BlockComment { .. } | TokenKind::Whitespace)
}))
}

/// Returns all the comments a given span contains
///
/// Comments are returned wrapped with their relevant delimiters
Expand Down
18 changes: 18 additions & 0 deletions tests/ui/collapsible_else_if.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,21 @@ fn issue_7318() {
}else if false {}
//~^^^ collapsible_else_if
}

fn issue14799() {
use std::ops::ControlFlow;

let c: ControlFlow<_, ()> = ControlFlow::Break(Some(42));
if let ControlFlow::Break(Some(_)) = c {
todo!();
} else {
#[cfg(target_os = "freebsd")]
todo!();

if let ControlFlow::Break(None) = c {
todo!();
} else {
todo!();
}
}
}
18 changes: 18 additions & 0 deletions tests/ui/collapsible_else_if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,21 @@ fn issue_7318() {
}
//~^^^ collapsible_else_if
}

fn issue14799() {
use std::ops::ControlFlow;

let c: ControlFlow<_, ()> = ControlFlow::Break(Some(42));
if let ControlFlow::Break(Some(_)) = c {
todo!();
} else {
#[cfg(target_os = "freebsd")]
todo!();

if let ControlFlow::Break(None) = c {
todo!();
} else {
todo!();
}
}
}