Skip to content

Commit 9e50049

Browse files
committed
Split possible_missing_else from suspicious_else_formatting.
1 parent fd7076b commit 9e50049

File tree

5 files changed

+38
-8
lines changed

5 files changed

+38
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6260,6 +6260,7 @@ Released 2018-09-13
62606260
[`pointers_in_nomem_asm_block`]: https://rust-lang.github.io/rust-clippy/master/index.html#pointers_in_nomem_asm_block
62616261
[`positional_named_format_parameters`]: https://rust-lang.github.io/rust-clippy/master/index.html#positional_named_format_parameters
62626262
[`possible_missing_comma`]: https://rust-lang.github.io/rust-clippy/master/index.html#possible_missing_comma
6263+
[`possible_missing_else`]: https://rust-lang.github.io/rust-clippy/master/index.html#possible_missing_else
62636264
[`precedence`]: https://rust-lang.github.io/rust-clippy/master/index.html#precedence
62646265
[`precedence_bits`]: https://rust-lang.github.io/rust-clippy/master/index.html#precedence_bits
62656266
[`print_in_format_impl`]: https://rust-lang.github.io/rust-clippy/master/index.html#print_in_format_impl

clippy_lints/src/declared_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ pub static LINTS: &[&::declare_clippy_lint::LintInfo] = &[
178178
crate::format_impl::RECURSIVE_FORMAT_IMPL_INFO,
179179
crate::format_push_string::FORMAT_PUSH_STRING_INFO,
180180
crate::formatting::POSSIBLE_MISSING_COMMA_INFO,
181+
crate::formatting::POSSIBLE_MISSING_ELSE_INFO,
181182
crate::formatting::SUSPICIOUS_ASSIGNMENT_FORMATTING_INFO,
182183
crate::formatting::SUSPICIOUS_ELSE_FORMATTING_INFO,
183184
crate::formatting::SUSPICIOUS_UNARY_OP_FORMATTING_INFO,

clippy_lints/src/formatting.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,31 @@ declare_clippy_lint! {
9191
"suspicious formatting of `else`"
9292
}
9393

94+
declare_clippy_lint! {
95+
/// ### What it does
96+
/// Checks for an `if` expression followed by either a block or another `if` that
97+
/// looks like it should have an `else` between them.
98+
///
99+
/// ### Why is this bad?
100+
/// This is probably some refactoring remnant, even if the code is correct, it
101+
/// might look confusing.
102+
///
103+
/// ### Example
104+
/// ```rust,ignore
105+
/// if foo {
106+
/// } { // looks like an `else` is missing here
107+
/// }
108+
///
109+
/// if foo {
110+
/// } if bar { // looks like an `else` is missing here
111+
/// }
112+
/// ```
113+
#[clippy::version = "1.90.0"]
114+
pub POSSIBLE_MISSING_ELSE,
115+
suspicious,
116+
"possibly missing `else`"
117+
}
118+
94119
declare_clippy_lint! {
95120
/// ### What it does
96121
/// Checks for possible missing comma in an array. It lints if
@@ -116,6 +141,7 @@ declare_lint_pass!(Formatting => [
116141
SUSPICIOUS_ASSIGNMENT_FORMATTING,
117142
SUSPICIOUS_UNARY_OP_FORMATTING,
118143
SUSPICIOUS_ELSE_FORMATTING,
144+
POSSIBLE_MISSING_ELSE,
119145
POSSIBLE_MISSING_COMMA
120146
]);
121147

@@ -307,7 +333,7 @@ fn check_missing_else(cx: &EarlyContext<'_>, first: &Expr, second: &Expr) {
307333

308334
span_lint_and_note(
309335
cx,
310-
SUSPICIOUS_ELSE_FORMATTING,
336+
POSSIBLE_MISSING_ELSE,
311337
else_span,
312338
format!("this looks like {looks_like} but the `else` is missing"),
313339
None,

tests/ui/suspicious_else_formatting.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@aux-build:proc_macro_suspicious_else_formatting.rs
22

3-
#![warn(clippy::suspicious_else_formatting)]
3+
#![warn(clippy::suspicious_else_formatting, clippy::possible_missing_else)]
44
#![allow(
55
clippy::if_same_then_else,
66
clippy::let_unit_value,
@@ -20,20 +20,20 @@ fn main() {
2020
// weird `else` formatting:
2121
if foo() {
2222
} {
23-
//~^ suspicious_else_formatting
23+
//~^ possible_missing_else
2424
}
2525

2626
if foo() {
2727
} if foo() {
28-
//~^ suspicious_else_formatting
28+
//~^ possible_missing_else
2929
}
3030

3131
let _ = { // if as the last expression
3232
let _ = 0;
3333

3434
if foo() {
3535
} if foo() {
36-
//~^ suspicious_else_formatting
36+
//~^ possible_missing_else
3737
}
3838
else {
3939
}
@@ -42,7 +42,7 @@ fn main() {
4242
let _ = { // if in the middle of a block
4343
if foo() {
4444
} if foo() {
45-
//~^ suspicious_else_formatting
45+
//~^ possible_missing_else
4646
}
4747
else {
4848
}

tests/ui/suspicious_else_formatting.stderr

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ LL | } {
55
| ^
66
|
77
= note: to remove this lint, add the missing `else` or add a new line before the next block
8-
= note: `-D clippy::suspicious-else-formatting` implied by `-D warnings`
9-
= help: to override `-D warnings` add `#[allow(clippy::suspicious_else_formatting)]`
8+
= note: `-D clippy::possible-missing-else` implied by `-D warnings`
9+
= help: to override `-D warnings` add `#[allow(clippy::possible_missing_else)]`
1010

1111
error: this looks like an `else if` but the `else` is missing
1212
--> tests/ui/suspicious_else_formatting.rs:27:6
@@ -41,6 +41,8 @@ LL | | {
4141
| |____^
4242
|
4343
= note: to remove this lint, remove the `else` or remove the new line between `else` and `{..}`
44+
= note: `-D clippy::suspicious-else-formatting` implied by `-D warnings`
45+
= help: to override `-D warnings` add `#[allow(clippy::suspicious_else_formatting)]`
4446

4547
error: this is an `else if` but the formatting might hide it
4648
--> tests/ui/suspicious_else_formatting.rs:67:6

0 commit comments

Comments
 (0)