Skip to content

Commit 5414eae

Browse files
authored
Rollup merge of #74324 - flip1995:clippyup, r=Manishearth
Update Clippy ~~I'm not sure, if we can/should land this before beta is branched.~~ (Nvm, beta is already branched) The last Clippy update was 3 weeks ago: #73660 This includes, besides other minor things: - New lints - One lint deprecation - One lint was moved to pedantic - Some FP fixes - I think an ICE fix? cc @Mark-Simulacrum r? @Manishearth --- We probably should also think of some process when and how often we should sync Clippy to the rust repo, so that we don't end up with those huge updates. Maybe every 2 weeks? Or even every week? cc @rust-lang/clippy
2 parents 2fb307a + f82ac4d commit 5414eae

File tree

143 files changed

+4316
-1083
lines changed

Some content is hidden

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

143 files changed

+4316
-1083
lines changed

src/tools/clippy/.github/ISSUE_TEMPLATE/new_lint.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ labels: L-lint
1212

1313
- Kind: *See <https://github.com/rust-lang/rust-clippy/blob/master/README.md#clippy> for list of lint kinds*
1414

15-
*What benefit of this lint over old code?*
15+
*What is the advantage of the recommended code over the original code*
1616

1717
For example:
1818
- Remove bounce checking inserted by ...

src/tools/clippy/.github/workflows/clippy_bors.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,8 @@ jobs:
240240
- 'Geal/nom'
241241
- 'rust-lang/stdarch'
242242
- 'serde-rs/serde'
243-
- 'chronotope/chrono'
243+
# FIXME: chrono currently cannot be compiled with `--all-targets`
244+
# - 'chronotope/chrono'
244245
- 'hyperium/hyper'
245246
- 'rust-random/rand'
246247
- 'rust-lang/futures-rs'

src/tools/clippy/CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1352,6 +1352,7 @@ Released 2018-09-13
13521352
[`bad_bit_mask`]: https://rust-lang.github.io/rust-clippy/master/index.html#bad_bit_mask
13531353
[`bind_instead_of_map`]: https://rust-lang.github.io/rust-clippy/master/index.html#bind_instead_of_map
13541354
[`blacklisted_name`]: https://rust-lang.github.io/rust-clippy/master/index.html#blacklisted_name
1355+
[`blanket_clippy_restriction_lints`]: https://rust-lang.github.io/rust-clippy/master/index.html#blanket_clippy_restriction_lints
13551356
[`blocks_in_if_conditions`]: https://rust-lang.github.io/rust-clippy/master/index.html#blocks_in_if_conditions
13561357
[`bool_comparison`]: https://rust-lang.github.io/rust-clippy/master/index.html#bool_comparison
13571358
[`borrow_interior_mutable_const`]: https://rust-lang.github.io/rust-clippy/master/index.html#borrow_interior_mutable_const
@@ -1508,9 +1509,11 @@ Released 2018-09-13
15081509
[`map_clone`]: https://rust-lang.github.io/rust-clippy/master/index.html#map_clone
15091510
[`map_entry`]: https://rust-lang.github.io/rust-clippy/master/index.html#map_entry
15101511
[`map_flatten`]: https://rust-lang.github.io/rust-clippy/master/index.html#map_flatten
1512+
[`map_identity`]: https://rust-lang.github.io/rust-clippy/master/index.html#map_identity
15111513
[`map_unwrap_or`]: https://rust-lang.github.io/rust-clippy/master/index.html#map_unwrap_or
15121514
[`match_as_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#match_as_ref
15131515
[`match_bool`]: https://rust-lang.github.io/rust-clippy/master/index.html#match_bool
1516+
[`match_like_matches_macro`]: https://rust-lang.github.io/rust-clippy/master/index.html#match_like_matches_macro
15141517
[`match_on_vec_items`]: https://rust-lang.github.io/rust-clippy/master/index.html#match_on_vec_items
15151518
[`match_overlapping_arm`]: https://rust-lang.github.io/rust-clippy/master/index.html#match_overlapping_arm
15161519
[`match_ref_pats`]: https://rust-lang.github.io/rust-clippy/master/index.html#match_ref_pats
@@ -1575,6 +1578,7 @@ Released 2018-09-13
15751578
[`op_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#op_ref
15761579
[`option_as_ref_deref`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_as_ref_deref
15771580
[`option_env_unwrap`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_env_unwrap
1581+
[`option_if_let_else`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_if_let_else
15781582
[`option_map_or_none`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_map_or_none
15791583
[`option_map_unit_fn`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_map_unit_fn
15801584
[`option_option`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_option
@@ -1586,6 +1590,7 @@ Released 2018-09-13
15861590
[`panicking_unwrap`]: https://rust-lang.github.io/rust-clippy/master/index.html#panicking_unwrap
15871591
[`partialeq_ne_impl`]: https://rust-lang.github.io/rust-clippy/master/index.html#partialeq_ne_impl
15881592
[`path_buf_push_overwrite`]: https://rust-lang.github.io/rust-clippy/master/index.html#path_buf_push_overwrite
1593+
[`pattern_type_mismatch`]: https://rust-lang.github.io/rust-clippy/master/index.html#pattern_type_mismatch
15891594
[`possible_missing_comma`]: https://rust-lang.github.io/rust-clippy/master/index.html#possible_missing_comma
15901595
[`precedence`]: https://rust-lang.github.io/rust-clippy/master/index.html#precedence
15911596
[`print_literal`]: https://rust-lang.github.io/rust-clippy/master/index.html#print_literal
@@ -1612,6 +1617,7 @@ Released 2018-09-13
16121617
[`redundant_static_lifetimes`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_static_lifetimes
16131618
[`ref_in_deref`]: https://rust-lang.github.io/rust-clippy/master/index.html#ref_in_deref
16141619
[`regex_macro`]: https://rust-lang.github.io/rust-clippy/master/index.html#regex_macro
1620+
[`repeat_once`]: https://rust-lang.github.io/rust-clippy/master/index.html#repeat_once
16151621
[`replace_consts`]: https://rust-lang.github.io/rust-clippy/master/index.html#replace_consts
16161622
[`rest_pat_in_fully_bound_structs`]: https://rust-lang.github.io/rust-clippy/master/index.html#rest_pat_in_fully_bound_structs
16171623
[`result_map_or_into_option`]: https://rust-lang.github.io/rust-clippy/master/index.html#result_map_or_into_option

src/tools/clippy/CONTRIBUTING.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ this to work, you will need the fix of `git subtree` available
245245
[here][gitgitgadget-pr].
246246
247247
[gitgitgadget-pr]: https://github.com/gitgitgadget/git/pull/493
248-
[subtree]: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#external-dependencies-subtree
248+
[subtree]: https://rustc-dev-guide.rust-lang.org/contributing.html#external-dependencies-subtree
249249
[`rust-lang/rust`]: https://github.com/rust-lang/rust
250250
251251
## Issue and PR triage

src/tools/clippy/clippy_lints/src/attrs.rs

+76-39
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
33
use crate::reexport::Name;
44
use crate::utils::{
5-
first_line_of_span, is_present_in_source, match_def_path, paths, snippet_opt, span_lint, span_lint_and_sugg,
6-
span_lint_and_then, without_block_comments,
5+
first_line_of_span, is_present_in_source, match_def_path, paths, snippet_opt, span_lint, span_lint_and_help,
6+
span_lint_and_sugg, span_lint_and_then, without_block_comments,
77
};
88
use if_chain::if_chain;
99
use rustc_ast::ast::{AttrKind, AttrStyle, Attribute, Lit, LitKind, MetaItemKind, NestedMetaItem};
@@ -17,7 +17,7 @@ use rustc_middle::lint::in_external_macro;
1717
use rustc_middle::ty;
1818
use rustc_session::{declare_lint_pass, declare_tool_lint};
1919
use rustc_span::source_map::Span;
20-
use rustc_span::symbol::Symbol;
20+
use rustc_span::symbol::{Symbol, SymbolStr};
2121
use semver::Version;
2222

2323
static UNIX_SYSTEMS: &[&str] = &[
@@ -182,6 +182,29 @@ declare_clippy_lint! {
182182
"unknown_lints for scoped Clippy lints"
183183
}
184184

185+
declare_clippy_lint! {
186+
/// **What it does:** Checks for `warn`/`deny`/`forbid` attributes targeting the whole clippy::restriction category.
187+
///
188+
/// **Why is this bad?** Restriction lints sometimes are in contrast with other lints or even go against idiomatic rust.
189+
/// These lints should only be enabled on a lint-by-lint basis and with careful consideration.
190+
///
191+
/// **Known problems:** None.
192+
///
193+
/// **Example:**
194+
/// Bad:
195+
/// ```rust
196+
/// #![deny(clippy::restriction)]
197+
/// ```
198+
///
199+
/// Good:
200+
/// ```rust
201+
/// #![deny(clippy::as_conversions)]
202+
/// ```
203+
pub BLANKET_CLIPPY_RESTRICTION_LINTS,
204+
style,
205+
"enabling the complete restriction group"
206+
}
207+
185208
declare_clippy_lint! {
186209
/// **What it does:** Checks for `#[cfg_attr(rustfmt, rustfmt_skip)]` and suggests to replace it
187210
/// with `#[rustfmt::skip]`.
@@ -249,15 +272,17 @@ declare_lint_pass!(Attributes => [
249272
DEPRECATED_SEMVER,
250273
USELESS_ATTRIBUTE,
251274
UNKNOWN_CLIPPY_LINTS,
275+
BLANKET_CLIPPY_RESTRICTION_LINTS,
252276
]);
253277

254278
impl<'tcx> LateLintPass<'tcx> for Attributes {
255279
fn check_attribute(&mut self, cx: &LateContext<'tcx>, attr: &'tcx Attribute) {
256280
if let Some(items) = &attr.meta_item_list() {
257281
if let Some(ident) = attr.ident() {
258-
match &*ident.as_str() {
282+
let ident = &*ident.as_str();
283+
match ident {
259284
"allow" | "warn" | "deny" | "forbid" => {
260-
check_clippy_lint_names(cx, items);
285+
check_clippy_lint_names(cx, ident, items);
261286
},
262287
_ => {},
263288
}
@@ -363,38 +388,43 @@ impl<'tcx> LateLintPass<'tcx> for Attributes {
363388
}
364389
}
365390

366-
#[allow(clippy::single_match_else)]
367-
fn check_clippy_lint_names(cx: &LateContext<'_>, items: &[NestedMetaItem]) {
368-
let lint_store = cx.lints();
369-
for lint in items {
391+
fn check_clippy_lint_names(cx: &LateContext<'_>, ident: &str, items: &[NestedMetaItem]) {
392+
fn extract_name(lint: &NestedMetaItem) -> Option<SymbolStr> {
370393
if_chain! {
371394
if let Some(meta_item) = lint.meta_item();
372395
if meta_item.path.segments.len() > 1;
373396
if let tool_name = meta_item.path.segments[0].ident;
374397
if tool_name.as_str() == "clippy";
375-
let name = meta_item.path.segments.last().unwrap().ident.name;
376-
if let CheckLintNameResult::Tool(Err((None, _))) = lint_store.check_lint_name(
377-
&name.as_str(),
378-
Some(tool_name.name),
379-
);
398+
let lint_name = meta_item.path.segments.last().unwrap().ident.name;
380399
then {
400+
return Some(lint_name.as_str());
401+
}
402+
}
403+
None
404+
}
405+
406+
let lint_store = cx.lints();
407+
for lint in items {
408+
if let Some(lint_name) = extract_name(lint) {
409+
if let CheckLintNameResult::Tool(Err((None, _))) =
410+
lint_store.check_lint_name(&lint_name, Some(sym!(clippy)))
411+
{
381412
span_lint_and_then(
382413
cx,
383414
UNKNOWN_CLIPPY_LINTS,
384415
lint.span(),
385-
&format!("unknown clippy lint: clippy::{}", name),
416+
&format!("unknown clippy lint: clippy::{}", lint_name),
386417
|diag| {
387-
let name_lower = name.as_str().to_lowercase();
388-
let symbols = lint_store.get_lints().iter().map(
389-
|l| Symbol::intern(&l.name_lower())
390-
).collect::<Vec<_>>();
391-
let sugg = find_best_match_for_name(
392-
symbols.iter(),
393-
&format!("clippy::{}", name_lower),
394-
None,
395-
);
396-
if name.as_str().chars().any(char::is_uppercase)
397-
&& lint_store.find_lints(&format!("clippy::{}", name_lower)).is_ok() {
418+
let name_lower = lint_name.to_lowercase();
419+
let symbols = lint_store
420+
.get_lints()
421+
.iter()
422+
.map(|l| Symbol::intern(&l.name_lower()))
423+
.collect::<Vec<_>>();
424+
let sugg = find_best_match_for_name(symbols.iter(), &format!("clippy::{}", name_lower), None);
425+
if lint_name.chars().any(char::is_uppercase)
426+
&& lint_store.find_lints(&format!("clippy::{}", name_lower)).is_ok()
427+
{
398428
diag.span_suggestion(
399429
lint.span(),
400430
"lowercase the lint name",
@@ -409,10 +439,19 @@ fn check_clippy_lint_names(cx: &LateContext<'_>, items: &[NestedMetaItem]) {
409439
Applicability::MachineApplicable,
410440
);
411441
}
412-
}
442+
},
443+
);
444+
} else if lint_name == "restriction" && ident != "allow" {
445+
span_lint_and_help(
446+
cx,
447+
BLANKET_CLIPPY_RESTRICTION_LINTS,
448+
lint.span(),
449+
"restriction lints are not meant to be all enabled",
450+
None,
451+
"try enabling only the lints you really need",
413452
);
414453
}
415-
};
454+
}
416455
}
417456
}
418457

@@ -442,15 +481,14 @@ fn is_relevant_trait(cx: &LateContext<'_>, item: &TraitItem<'_>) -> bool {
442481
}
443482

444483
fn is_relevant_block(cx: &LateContext<'_>, tables: &ty::TypeckTables<'_>, block: &Block<'_>) -> bool {
445-
if let Some(stmt) = block.stmts.first() {
446-
match &stmt.kind {
484+
block.stmts.first().map_or(
485+
block.expr.as_ref().map_or(false, |e| is_relevant_expr(cx, tables, e)),
486+
|stmt| match &stmt.kind {
447487
StmtKind::Local(_) => true,
448488
StmtKind::Expr(expr) | StmtKind::Semi(expr) => is_relevant_expr(cx, tables, expr),
449489
_ => false,
450-
}
451-
} else {
452-
block.expr.as_ref().map_or(false, |e| is_relevant_expr(cx, tables, e))
453-
}
490+
},
491+
)
454492
}
455493

456494
fn is_relevant_expr(cx: &LateContext<'_>, tables: &ty::TypeckTables<'_>, expr: &Expr<'_>) -> bool {
@@ -460,11 +498,10 @@ fn is_relevant_expr(cx: &LateContext<'_>, tables: &ty::TypeckTables<'_>, expr: &
460498
ExprKind::Ret(None) | ExprKind::Break(_, None) => false,
461499
ExprKind::Call(path_expr, _) => {
462500
if let ExprKind::Path(qpath) = &path_expr.kind {
463-
if let Some(fun_id) = tables.qpath_res(qpath, path_expr.hir_id).opt_def_id() {
464-
!match_def_path(cx, fun_id, &paths::BEGIN_PANIC)
465-
} else {
466-
true
467-
}
501+
tables
502+
.qpath_res(qpath, path_expr.hir_id)
503+
.opt_def_id()
504+
.map_or(true, |fun_id| !match_def_path(cx, fun_id, &paths::BEGIN_PANIC))
468505
} else {
469506
true
470507
}

src/tools/clippy/clippy_lints/src/await_holding_lock.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ declare_clippy_lint! {
1111
/// non-async-aware MutexGuard.
1212
///
1313
/// **Why is this bad?** The Mutex types found in syd::sync and parking_lot
14-
/// are not designed to operator in an async context across await points.
14+
/// are not designed to operate in an async context across await points.
1515
///
1616
/// There are two potential solutions. One is to use an asynx-aware Mutex
1717
/// type. Many asynchronous foundation crates provide such a Mutex type. The

src/tools/clippy/clippy_lints/src/collapsible_if.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ fn check_collapsible_maybe_if_let(cx: &EarlyContext<'_>, else_: &ast::Expr) {
115115
COLLAPSIBLE_IF,
116116
block.span,
117117
"this `else { if .. }` block can be collapsed",
118-
"try",
118+
"collapse nested if block",
119119
snippet_block_with_applicability(cx, else_.span, "..", Some(block.span), &mut applicability).into_owned(),
120120
applicability,
121121
);
@@ -142,7 +142,7 @@ fn check_collapsible_no_if_let(cx: &EarlyContext<'_>, expr: &ast::Expr, check: &
142142
let rhs = Sugg::ast(cx, check_inner, "..");
143143
diag.span_suggestion(
144144
expr.span,
145-
"try",
145+
"collapse nested if block",
146146
format!(
147147
"if {} {}",
148148
lhs.and(&rhs),

src/tools/clippy/clippy_lints/src/comparison_chain.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,5 @@ impl<'tcx> LateLintPass<'tcx> for ComparisonChain {
122122
}
123123

124124
fn kind_is_cmp(kind: BinOpKind) -> bool {
125-
match kind {
126-
BinOpKind::Lt | BinOpKind::Gt | BinOpKind::Eq => true,
127-
_ => false,
128-
}
125+
matches!(kind, BinOpKind::Lt | BinOpKind::Gt | BinOpKind::Eq)
129126
}

src/tools/clippy/clippy_lints/src/deprecated_lints.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -153,5 +153,13 @@ declare_deprecated_lint! {
153153
///
154154
/// **Deprecation reason:** Associated-constants are now preferred.
155155
pub REPLACE_CONSTS,
156-
"associated-constants `MIN`/`MAX` of integers are prefer to `{min,max}_value()` and module constants"
156+
"associated-constants `MIN`/`MAX` of integers are prefered to `{min,max}_value()` and module constants"
157+
}
158+
159+
declare_deprecated_lint! {
160+
/// **What it does:** Nothing. This lint has been deprecated.
161+
///
162+
/// **Deprecation reason:** The regex! macro does not exist anymore.
163+
pub REGEX_MACRO,
164+
"the regex! macro has been removed from the regex crate in 2018"
157165
}

src/tools/clippy/clippy_lints/src/dereference.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,10 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing {
7373
fn lint_deref(cx: &LateContext<'_>, method_name: &str, call_expr: &Expr<'_>, var_span: Span, expr_span: Span) {
7474
match method_name {
7575
"deref" => {
76-
if cx.tcx.lang_items().deref_trait().map_or(false, |id| {
76+
let impls_deref_trait = cx.tcx.lang_items().deref_trait().map_or(false, |id| {
7777
implements_trait(cx, cx.tables().expr_ty(&call_expr), id, &[])
78-
}) {
78+
});
79+
if impls_deref_trait {
7980
span_lint_and_sugg(
8081
cx,
8182
EXPLICIT_DEREF_METHODS,
@@ -88,9 +89,10 @@ fn lint_deref(cx: &LateContext<'_>, method_name: &str, call_expr: &Expr<'_>, var
8889
}
8990
},
9091
"deref_mut" => {
91-
if cx.tcx.lang_items().deref_mut_trait().map_or(false, |id| {
92+
let impls_deref_mut_trait = cx.tcx.lang_items().deref_mut_trait().map_or(false, |id| {
9293
implements_trait(cx, cx.tables().expr_ty(&call_expr), id, &[])
93-
}) {
94+
});
95+
if impls_deref_mut_trait {
9496
span_lint_and_sugg(
9597
cx,
9698
EXPLICIT_DEREF_METHODS,

src/tools/clippy/clippy_lints/src/eq_op.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -214,20 +214,20 @@ impl<'tcx> LateLintPass<'tcx> for EqOp {
214214
}
215215

216216
fn is_valid_operator(op: BinOp) -> bool {
217-
match op.node {
217+
matches!(
218+
op.node,
218219
BinOpKind::Sub
219-
| BinOpKind::Div
220-
| BinOpKind::Eq
221-
| BinOpKind::Lt
222-
| BinOpKind::Le
223-
| BinOpKind::Gt
224-
| BinOpKind::Ge
225-
| BinOpKind::Ne
226-
| BinOpKind::And
227-
| BinOpKind::Or
228-
| BinOpKind::BitXor
229-
| BinOpKind::BitAnd
230-
| BinOpKind::BitOr => true,
231-
_ => false,
232-
}
220+
| BinOpKind::Div
221+
| BinOpKind::Eq
222+
| BinOpKind::Lt
223+
| BinOpKind::Le
224+
| BinOpKind::Gt
225+
| BinOpKind::Ge
226+
| BinOpKind::Ne
227+
| BinOpKind::And
228+
| BinOpKind::Or
229+
| BinOpKind::BitXor
230+
| BinOpKind::BitAnd
231+
| BinOpKind::BitOr
232+
)
233233
}

src/tools/clippy/clippy_lints/src/escape.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,7 @@ fn is_argument(map: rustc_middle::hir::map::Map<'_>, id: HirId) -> bool {
105105
_ => return false,
106106
}
107107

108-
match map.find(map.get_parent_node(id)) {
109-
Some(Node::Param(_)) => true,
110-
_ => false,
111-
}
108+
matches!(map.find(map.get_parent_node(id)), Some(Node::Param(_)))
112109
}
113110

114111
impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {

0 commit comments

Comments
 (0)