Skip to content

Commit 8f1b4bb

Browse files
authored
New lint: unnecessary_semicolon (#14032)
This lint detects and removes the unnecessary semicolon after a `match` or `if` statement returning `()`. It seems to be quite a common "mistake", given the number of hits (88) we had in the Clippy sources themselves. The lint doesn't bother about loops, as `rustfmt` already removes the extra semicolon. It doesn't handle blocks either, as an extra block level, followed or not by a semicolon, is likely intentional. I propose to put the lint in `pedantic`, as putting it in `style` seems quite hazardous given the number of hits. Note: there exists a `redundant-semicolon` lint in the compiler, but it is an early lint and cannot check that the expression evaluates to `()`, so it ignores the cases we're handling here. ---- changelog: [`unnecessary_semicolon`]: new lint
2 parents 2280b8a + 3a7f50f commit 8f1b4bb

File tree

83 files changed

+236
-88
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+236
-88
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6174,6 +6174,7 @@ Released 2018-09-13
61746174
[`unnecessary_safety_comment`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_safety_comment
61756175
[`unnecessary_safety_doc`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_safety_doc
61766176
[`unnecessary_self_imports`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_self_imports
6177+
[`unnecessary_semicolon`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_semicolon
61776178
[`unnecessary_sort_by`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_sort_by
61786179
[`unnecessary_struct_initialization`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_struct_initialization
61796180
[`unnecessary_to_owned`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_to_owned

clippy_dev/src/setup/intellij.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ fn check_and_get_rustc_dir(rustc_path: &str) -> Result<PathBuf, ()> {
6262
eprintln!("error: unable to get the absolute path of rustc ({err})");
6363
return Err(());
6464
},
65-
};
65+
}
6666
}
6767

6868
let path = path.join("compiler");

clippy_dev/src/update_lints.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@ fn try_rename_file(old_name: &Path, new_name: &Path) -> bool {
842842
Ok(file) => drop(file),
843843
Err(e) if matches!(e.kind(), io::ErrorKind::AlreadyExists | io::ErrorKind::NotFound) => return false,
844844
Err(e) => panic_file(e, new_name, "create"),
845-
};
845+
}
846846
match fs::rename(old_name, new_name) {
847847
Ok(()) => true,
848848
Err(e) => {

clippy_lints/src/assigning_clones.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ fn build_sugg<'tcx>(
257257
// The receiver may have been a value type, so we need to add an `&` to
258258
// be sure the argument to clone_from will be a reference.
259259
arg_sugg = arg_sugg.addr();
260-
};
260+
}
261261

262262
format!("{receiver_sugg}.clone_from({arg_sugg})")
263263
},

clippy_lints/src/attrs/mixed_attributes_style.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub(super) fn check(cx: &EarlyContext<'_>, item_span: Span, attrs: &[Attribute])
6060
}
6161
outer_attr_kind.insert(kind);
6262
},
63-
};
63+
}
6464
}
6565
}
6666

clippy_lints/src/casts/cast_possible_wrap.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,6 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_from: Ty<'_>, ca
8484
diag
8585
.note("`usize` and `isize` may be as small as 16 bits on some platforms")
8686
.note("for more information see https://doc.rust-lang.org/reference/types/numeric.html#machine-dependent-integer-types");
87-
};
87+
}
8888
});
8989
}

clippy_lints/src/casts/cast_sign_loss.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ fn expr_muldiv_sign(cx: &LateContext<'_>, expr: &Expr<'_>) -> Sign {
205205
// - uncertain if there are any uncertain values (because they could be negative or positive),
206206
Sign::Uncertain => return Sign::Uncertain,
207207
Sign::ZeroOrPositive => (),
208-
};
208+
}
209209
}
210210

211211
// A mul/div is:
@@ -236,7 +236,7 @@ fn expr_add_sign(cx: &LateContext<'_>, expr: &Expr<'_>) -> Sign {
236236
// - uncertain if there are any uncertain values (because they could be negative or positive),
237237
Sign::Uncertain => return Sign::Uncertain,
238238
Sign::ZeroOrPositive => positive_count += 1,
239-
};
239+
}
240240
}
241241

242242
// A sum is:

clippy_lints/src/checked_conversions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ fn get_types_from_cast<'a>(
273273
},
274274
_ => {},
275275
}
276-
};
276+
}
277277
None
278278
}
279279

clippy_lints/src/declared_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,7 @@ pub static LINTS: &[&crate::LintInfo] = &[
757757
crate::unnecessary_map_on_constructor::UNNECESSARY_MAP_ON_CONSTRUCTOR_INFO,
758758
crate::unnecessary_owned_empty_strings::UNNECESSARY_OWNED_EMPTY_STRINGS_INFO,
759759
crate::unnecessary_self_imports::UNNECESSARY_SELF_IMPORTS_INFO,
760+
crate::unnecessary_semicolon::UNNECESSARY_SEMICOLON_INFO,
760761
crate::unnecessary_struct_initialization::UNNECESSARY_STRUCT_INITIALIZATION_INFO,
761762
crate::unnecessary_wraps::UNNECESSARY_WRAPS_INFO,
762763
crate::unneeded_struct_pattern::UNNEEDED_STRUCT_PATTERN_INFO,

clippy_lints/src/default_constructed_unit_structs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,6 @@ impl LateLintPass<'_> for DefaultConstructedUnitStructs {
8080
String::new(),
8181
Applicability::MachineApplicable,
8282
);
83-
};
83+
}
8484
}
8585
}

0 commit comments

Comments
 (0)