Skip to content

Commit 7feb191

Browse files
committed
Auto merge of rust-lang#125764 - flip1995:clippy-subtree-update, r=Manishearth
Clippy subtree update r? `@Manishearth`
2 parents cfb7304 + 9b320af commit 7feb191

File tree

128 files changed

+1422
-676
lines changed

Some content is hidden

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

128 files changed

+1422
-676
lines changed

src/tools/clippy/book/src/configuration.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ Very rarely, you may wish to prevent Clippy from evaluating certain sections of
133133
`clippy` cfg is not set. You may need to provide a stub so that the code compiles:
134134

135135
```rust
136-
#[cfg(not(clippy)]
136+
#[cfg(not(clippy))]
137137
include!(concat!(env!("OUT_DIR"), "/my_big_function-generated.rs"));
138138

139139
#[cfg(clippy)]

src/tools/clippy/book/src/development/adding_lints.md

+5
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,11 @@ declare_clippy_lint! {
587587
}
588588
```
589589

590+
If the lint is in the `restriction` group because it lints things that are not
591+
necessarily “bad” but are more of a style choice, then replace the
592+
“Why is this bad?” section heading with “Why restrict this?”, to avoid writing
593+
“Why is this bad? It isn't, but ...”.
594+
590595
Once your lint is merged, this documentation will show up in the [lint
591596
list][lint_list].
592597

src/tools/clippy/clippy_dev/src/new_lint.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -331,12 +331,17 @@ fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String {
331331
}
332332

333333
fn get_lint_declaration(name_upper: &str, category: &str) -> String {
334+
let justification_heading = if category == "restriction" {
335+
"Why restrict this?"
336+
} else {
337+
"Why is this bad?"
338+
};
334339
formatdoc!(
335340
r#"
336341
declare_clippy_lint! {{
337342
/// ### What it does
338343
///
339-
/// ### Why is this bad?
344+
/// ### {justification_heading}
340345
///
341346
/// ### Example
342347
/// ```no_run

src/tools/clippy/clippy_lints/src/absolute_paths.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ declare_clippy_lint! {
1212
/// ### What it does
1313
/// Checks for usage of items through absolute paths, like `std::env::current_dir`.
1414
///
15-
/// ### Why is this bad?
15+
/// ### Why restrict this?
1616
/// Many codebases have their own style when it comes to importing, but one that is seldom used
1717
/// is using absolute paths *everywhere*. This is generally considered unidiomatic, and you
1818
/// should add a `use` statement.

src/tools/clippy/clippy_lints/src/allow_attributes.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ declare_clippy_lint! {
1919
/// This lint only warns outer attributes (`#[allow]`), as inner attributes
2020
/// (`#![allow]`) are usually used to enable or disable lints on a global scale.
2121
///
22-
/// ### Why is this bad?
23-
/// `#[expect]` attributes suppress the lint emission, but emit a warning, if
22+
/// ### Why restrict this?
23+
/// `#[allow]` attributes can linger after their reason for existence is gone.
24+
/// `#[expect]` attributes suppress the lint emission, but emit a warning if
2425
/// the expectation is unfulfilled. This can be useful to be notified when the
25-
/// lint is no longer triggered.
26+
/// lint is no longer triggered, which may indicate the attribute can be removed.
2627
///
2728
/// ### Example
2829
/// ```rust,ignore

src/tools/clippy/clippy_lints/src/as_conversions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ declare_clippy_lint! {
1717
/// There is a good explanation the reason why this lint should work in this way and how it is useful
1818
/// [in this issue](https://github.com/rust-lang/rust-clippy/issues/5122).
1919
///
20-
/// ### Why is this bad?
20+
/// ### Why restrict this?
2121
/// `as` conversions will perform many kinds of
2222
/// conversions, including silently lossy conversions and dangerous coercions.
2323
/// There are cases when it makes sense to use `as`, so the lint is

src/tools/clippy/clippy_lints/src/asm_syntax.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,8 @@ declare_clippy_lint! {
6565
/// ### What it does
6666
/// Checks for usage of Intel x86 assembly syntax.
6767
///
68-
/// ### Why is this bad?
69-
/// The lint has been enabled to indicate a preference
70-
/// for AT&T x86 assembly syntax.
68+
/// ### Why restrict this?
69+
/// To enforce consistent use of AT&T x86 assembly syntax.
7170
///
7271
/// ### Example
7372
///
@@ -114,9 +113,8 @@ declare_clippy_lint! {
114113
/// ### What it does
115114
/// Checks for usage of AT&T x86 assembly syntax.
116115
///
117-
/// ### Why is this bad?
118-
/// The lint has been enabled to indicate a preference
119-
/// for Intel x86 assembly syntax.
116+
/// ### Why restrict this?
117+
/// To enforce consistent use of Intel x86 assembly syntax.
120118
///
121119
/// ### Example
122120
///

src/tools/clippy/clippy_lints/src/assertions_on_result_states.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,33 @@ declare_clippy_lint! {
1616
/// ### What it does
1717
/// Checks for `assert!(r.is_ok())` or `assert!(r.is_err())` calls.
1818
///
19-
/// ### Why is this bad?
20-
/// An assertion failure cannot output an useful message of the error.
19+
/// ### Why restrict this?
20+
/// This form of assertion does not show any of the information present in the `Result`
21+
/// other than which variant it isn’t.
2122
///
2223
/// ### Known problems
2324
/// The suggested replacement decreases the readability of code and log output.
2425
///
2526
/// ### Example
26-
/// ```rust,ignore
27+
/// ```rust,no_run
2728
/// # let r = Ok::<_, ()>(());
2829
/// assert!(r.is_ok());
29-
/// # let r = Err::<_, ()>(());
30+
/// # let r = Err::<(), _>(());
3031
/// assert!(r.is_err());
3132
/// ```
33+
///
34+
/// Use instead:
35+
///
36+
/// ```rust,no_run
37+
/// # let r = Ok::<_, ()>(());
38+
/// r.unwrap();
39+
/// # let r = Err::<(), _>(());
40+
/// r.unwrap_err();
41+
/// ```
3242
#[clippy::version = "1.64.0"]
3343
pub ASSERTIONS_ON_RESULT_STATES,
3444
restriction,
35-
"`assert!(r.is_ok())`/`assert!(r.is_err())` gives worse error message than directly calling `r.unwrap()`/`r.unwrap_err()`"
45+
"`assert!(r.is_ok())` or `assert!(r.is_err())` gives worse panic messages than directly calling `r.unwrap()` or `r.unwrap_err()`"
3646
}
3747

3848
declare_lint_pass!(AssertionsOnResultStates => [ASSERTIONS_ON_RESULT_STATES]);

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -309,9 +309,9 @@ declare_clippy_lint! {
309309
///
310310
/// (This requires the `lint_reasons` feature)
311311
///
312-
/// ### Why is this bad?
313-
/// Allowing a lint should always have a reason. This reason should be documented to
314-
/// ensure that others understand the reasoning
312+
/// ### Why restrict this?
313+
/// Justifying each `allow` helps readers understand the reasoning,
314+
/// and may allow removing `allow` attributes if their purpose is obsolete.
315315
///
316316
/// ### Example
317317
/// ```no_run

src/tools/clippy/clippy_lints/src/casts/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ declare_clippy_lint! {
303303
/// ### What it does
304304
/// Checks for casts of a function pointer to any integer type.
305305
///
306-
/// ### Why is this bad?
306+
/// ### Why restrict this?
307307
/// Casting a function pointer to an integer can have surprising results and can occur
308308
/// accidentally if parentheses are omitted from a function call. If you aren't doing anything
309309
/// low-level with function pointers then you can opt-out of casting functions to integers in
@@ -535,8 +535,8 @@ declare_clippy_lint! {
535535
/// ### What it does
536536
/// Checks for the usage of `as _` conversion using inferred type.
537537
///
538-
/// ### Why is this bad?
539-
/// The conversion might include lossy conversion and dangerous cast that might go
538+
/// ### Why restrict this?
539+
/// The conversion might include lossy conversion or a dangerous cast that might go
540540
/// undetected due to the type being inferred.
541541
///
542542
/// The lint is allowed by default as using `_` is less wordy than always specifying the type.

src/tools/clippy/clippy_lints/src/create_dir.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ declare_clippy_lint! {
1010
/// ### What it does
1111
/// Checks usage of `std::fs::create_dir` and suggest using `std::fs::create_dir_all` instead.
1212
///
13-
/// ### Why is this bad?
14-
/// Sometimes `std::fs::create_dir` is mistakenly chosen over `std::fs::create_dir_all`.
13+
/// ### Why restrict this?
14+
/// Sometimes `std::fs::create_dir` is mistakenly chosen over `std::fs::create_dir_all`,
15+
/// resulting in failure when more than one directory needs to be created or when the directory already exists.
16+
/// Crates which never need to specifically create a single directory may wish to prevent this mistake.
1517
///
1618
/// ### Example
1719
/// ```rust,ignore

src/tools/clippy/clippy_lints/src/dbg_macro.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ declare_clippy_lint! {
1414
/// ### What it does
1515
/// Checks for usage of the [`dbg!`](https://doc.rust-lang.org/std/macro.dbg.html) macro.
1616
///
17-
/// ### Why is this bad?
17+
/// ### Why restrict this?
1818
/// The `dbg!` macro is intended as a debugging tool. It should not be present in released
1919
/// software or committed to a version control system.
2020
///

src/tools/clippy/clippy_lints/src/default_numeric_fallback.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ declare_clippy_lint! {
2222
///
2323
/// See [RFC0212](https://github.com/rust-lang/rfcs/blob/master/text/0212-restore-int-fallback.md) for more information about the fallback.
2424
///
25-
/// ### Why is this bad?
26-
/// For those who are very careful about types, default numeric fallback
27-
/// can be a pitfall that cause unexpected runtime behavior.
25+
/// ### Why restrict this?
26+
/// To ensure that every numeric type is chosen explicitly rather than implicitly.
2827
///
2928
/// ### Known problems
3029
/// This lint can only be allowed at the function level or above.

src/tools/clippy/clippy_lints/src/default_union_representation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ declare_clippy_lint! {
1010
/// ### What it does
1111
/// Displays a warning when a union is declared with the default representation (without a `#[repr(C)]` attribute).
1212
///
13-
/// ### Why is this bad?
13+
/// ### Why restrict this?
1414
/// Unions in Rust have unspecified layout by default, despite many people thinking that they
1515
/// lay out each field at the start of the union (like C does). That is, there are no guarantees
1616
/// about the offset of the fields for unions with multiple non-ZST fields without an explicitly

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

+5-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_hir_and_then};
22
use clippy_utils::source::{snippet_with_applicability, snippet_with_context};
33
use clippy_utils::sugg::has_enclosing_paren;
44
use clippy_utils::ty::{implements_trait, is_manually_drop, peel_mid_ty_refs};
5-
use clippy_utils::{expr_use_ctxt, get_parent_expr, is_lint_allowed, path_to_local, DefinedTy, ExprUseNode};
5+
use clippy_utils::{
6+
expr_use_ctxt, get_parent_expr, is_block_like, is_lint_allowed, path_to_local, DefinedTy, ExprUseNode,
7+
};
68
use core::mem;
79
use rustc_ast::util::parser::{PREC_POSTFIX, PREC_PREFIX};
810
use rustc_data_structures::fx::FxIndexMap;
@@ -1038,14 +1040,8 @@ fn report<'tcx>(
10381040
);
10391041
},
10401042
State::ExplicitDeref { mutability } => {
1041-
if matches!(
1042-
expr.kind,
1043-
ExprKind::Block(..)
1044-
| ExprKind::ConstBlock(_)
1045-
| ExprKind::If(..)
1046-
| ExprKind::Loop(..)
1047-
| ExprKind::Match(..)
1048-
) && let ty::Ref(_, ty, _) = data.adjusted_ty.kind()
1043+
if is_block_like(expr)
1044+
&& let ty::Ref(_, ty, _) = data.adjusted_ty.kind()
10491045
&& ty.is_sized(cx.tcx, cx.param_env)
10501046
{
10511047
// Rustc bug: auto deref doesn't work on block expression when targeting sized types.

src/tools/clippy/clippy_lints/src/derive.rs

+21-10
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_note, span_lint_and_sugg, span_lint_and_then};
1+
use clippy_utils::diagnostics::{span_lint_and_note, span_lint_and_then, span_lint_hir_and_then};
22
use clippy_utils::ty::{implements_trait, implements_trait_with_env, is_copy};
33
use clippy_utils::{has_non_exhaustive_attr, is_lint_allowed, match_def_path, paths};
44
use rustc_errors::Applicability;
55
use rustc_hir::def_id::DefId;
66
use rustc_hir::intravisit::{walk_expr, walk_fn, walk_item, FnKind, Visitor};
77
use rustc_hir::{
8-
self as hir, BlockCheckMode, BodyId, Expr, ExprKind, FnDecl, Safety, Impl, Item, ItemKind, UnsafeSource,
8+
self as hir, BlockCheckMode, BodyId, Expr, ExprKind, FnDecl, Impl, Item, ItemKind, Safety, UnsafeSource,
99
};
1010
use rustc_lint::{LateContext, LateLintPass};
1111
use rustc_middle::hir::nested_filter;
1212
use rustc_middle::traits::Reveal;
1313
use rustc_middle::ty::{
14-
self, ClauseKind, GenericArgKind, GenericParamDefKind, ParamEnv, Upcast, TraitPredicate, Ty, TyCtxt,
14+
self, ClauseKind, GenericArgKind, GenericParamDefKind, ParamEnv, TraitPredicate, Ty, TyCtxt, Upcast,
1515
};
1616
use rustc_session::declare_lint_pass;
1717
use rustc_span::def_id::LocalDefId;
@@ -390,13 +390,17 @@ fn check_unsafe_derive_deserialize<'tcx>(
390390
.map(|imp_did| cx.tcx.hir().expect_item(imp_did.expect_local()))
391391
.any(|imp| has_unsafe(cx, imp))
392392
{
393-
span_lint_and_help(
393+
span_lint_hir_and_then(
394394
cx,
395395
UNSAFE_DERIVE_DESERIALIZE,
396+
adt_hir_id,
396397
item.span,
397398
"you are deriving `serde::Deserialize` on a type that has methods using `unsafe`",
398-
None,
399-
"consider implementing `serde::Deserialize` manually. See https://serde.rs/impl-deserialize.html",
399+
|diag| {
400+
diag.help(
401+
"consider implementing `serde::Deserialize` manually. See https://serde.rs/impl-deserialize.html",
402+
);
403+
},
400404
);
401405
}
402406
}
@@ -452,20 +456,27 @@ fn check_partial_eq_without_eq<'tcx>(cx: &LateContext<'tcx>, span: Span, trait_r
452456
&& !has_non_exhaustive_attr(cx.tcx, *adt)
453457
&& !ty_implements_eq_trait(cx.tcx, ty, eq_trait_def_id)
454458
&& let param_env = param_env_for_derived_eq(cx.tcx, adt.did(), eq_trait_def_id)
459+
&& let Some(local_def_id) = adt.did().as_local()
455460
// If all of our fields implement `Eq`, we can implement `Eq` too
456461
&& adt
457462
.all_fields()
458463
.map(|f| f.ty(cx.tcx, args))
459464
.all(|ty| implements_trait_with_env(cx.tcx, param_env, ty, eq_trait_def_id, None, &[]))
460465
{
461-
span_lint_and_sugg(
466+
span_lint_hir_and_then(
462467
cx,
463468
DERIVE_PARTIAL_EQ_WITHOUT_EQ,
469+
cx.tcx.local_def_id_to_hir_id(local_def_id),
464470
span.ctxt().outer_expn_data().call_site,
465471
"you are deriving `PartialEq` and can implement `Eq`",
466-
"consider deriving `Eq` as well",
467-
"PartialEq, Eq".to_string(),
468-
Applicability::MachineApplicable,
472+
|diag| {
473+
diag.span_suggestion(
474+
span.ctxt().outer_expn_data().call_site,
475+
"consider deriving `Eq` as well",
476+
"PartialEq, Eq",
477+
Applicability::MachineApplicable,
478+
);
479+
},
469480
);
470481
}
471482
}

src/tools/clippy/clippy_lints/src/disallowed_script_idents.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ declare_clippy_lint! {
2020
/// [aliases]: http://www.unicode.org/reports/tr24/tr24-31.html#Script_Value_Aliases
2121
/// [supported_scripts]: https://www.unicode.org/iso15924/iso15924-codes.html
2222
///
23-
/// ### Why is this bad?
23+
/// ### Why restrict this?
2424
/// It may be not desired to have many different scripts for
2525
/// identifiers in the codebase.
2626
///
27-
/// Note that if you only want to allow plain English, you might want to use
27+
/// Note that if you only want to allow typical English, you might want to use
2828
/// built-in [`non_ascii_idents`] lint instead.
2929
///
3030
/// [`non_ascii_idents`]: https://doc.rust-lang.org/rustc/lints/listing/allowed-by-default.html#non-ascii-idents

src/tools/clippy/clippy_lints/src/doc/markdown.rs

+25-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
1+
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
22
use clippy_utils::source::snippet_with_applicability;
33
use rustc_data_structures::fx::FxHashSet;
44
use rustc_errors::{Applicability, SuggestionStyle};
@@ -30,6 +30,7 @@ pub fn check(
3030
word = tmp_word;
3131
}
3232

33+
let original_len = word.len();
3334
word = word.trim_start_matches(trim_pattern);
3435

3536
// Remove leading or trailing single `:` which may be part of a sentence.
@@ -44,6 +45,25 @@ pub fn check(
4445
continue;
4546
}
4647

48+
// Ensure that all reachable matching closing parens are included as well.
49+
let size_diff = original_len - word.len();
50+
let mut open_parens = 0;
51+
let mut close_parens = 0;
52+
for c in word.chars() {
53+
if c == '(' {
54+
open_parens += 1;
55+
} else if c == ')' {
56+
close_parens += 1;
57+
}
58+
}
59+
while close_parens < open_parens
60+
&& let Some(tmp_word) = orig_word.get(size_diff..=(word.len() + size_diff))
61+
&& tmp_word.ends_with(')')
62+
{
63+
word = tmp_word;
64+
close_parens += 1;
65+
}
66+
4767
// Adjust for the current word
4868
let offset = word.as_ptr() as usize - text.as_ptr() as usize;
4969
let span = Span::new(
@@ -92,13 +112,15 @@ fn check_word(cx: &LateContext<'_>, word: &str, span: Span, code_level: isize, b
92112
if let Ok(url) = Url::parse(word) {
93113
// try to get around the fact that `foo::bar` parses as a valid URL
94114
if !url.cannot_be_a_base() {
95-
span_lint(
115+
span_lint_and_sugg(
96116
cx,
97117
DOC_MARKDOWN,
98118
span,
99119
"you should put bare URLs between `<`/`>` or make a proper Markdown link",
120+
"try",
121+
format!("<{word}>"),
122+
Applicability::MachineApplicable,
100123
);
101-
102124
return;
103125
}
104126
}

src/tools/clippy/clippy_lints/src/doc/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ declare_clippy_lint! {
261261
/// Checks for the doc comments of publicly visible
262262
/// safe functions and traits and warns if there is a `# Safety` section.
263263
///
264-
/// ### Why is this bad?
264+
/// ### Why restrict this?
265265
/// Safe functions and traits are safe to implement and therefore do not
266266
/// need to describe safety preconditions that users are required to uphold.
267267
///

0 commit comments

Comments
 (0)