Skip to content

Commit bb4781a

Browse files
committed
Auto merge of #97248 - xFrednet:clippyup, r=Manishearth
Clippyup This direction was simpler. All test Clippy pass locally 🙃 r? `@Manishearth`
2 parents 74b1369 + 6e87c24 commit bb4781a

File tree

240 files changed

+4453
-1715
lines changed

Some content is hidden

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

240 files changed

+4453
-1715
lines changed

Cargo.lock

+4-3
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ dependencies = [
627627

628628
[[package]]
629629
name = "clippy"
630-
version = "0.1.62"
630+
version = "0.1.63"
631631
dependencies = [
632632
"clippy_lints",
633633
"clippy_utils",
@@ -647,6 +647,7 @@ dependencies = [
647647
"serde",
648648
"syn",
649649
"tempfile",
650+
"termize",
650651
"tester",
651652
"tokio",
652653
]
@@ -667,7 +668,7 @@ dependencies = [
667668

668669
[[package]]
669670
name = "clippy_lints"
670-
version = "0.1.62"
671+
version = "0.1.63"
671672
dependencies = [
672673
"cargo_metadata",
673674
"clippy_utils",
@@ -688,7 +689,7 @@ dependencies = [
688689

689690
[[package]]
690691
name = "clippy_utils"
691-
version = "0.1.62"
692+
version = "0.1.63"
692693
dependencies = [
693694
"arrayvec",
694695
"if_chain",

src/tools/clippy/.github/deploy.sh

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ rm -rf out/master/ || exit 0
88
echo "Making the docs for master"
99
mkdir out/master/
1010
cp util/gh-pages/index.html out/master
11+
cp util/gh-pages/script.js out/master
1112
cp util/gh-pages/lints.json out/master
1213

1314
if [[ -n $TAG_NAME ]]; then

src/tools/clippy/CHANGELOG.md

+42-3
Large diffs are not rendered by default.

src/tools/clippy/CONTRIBUTING.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ and resolved paths.
6767

6868
[`T-AST`] issues will generally need you to match against a predefined syntax structure.
6969
To figure out how this syntax structure is encoded in the AST, it is recommended to run
70-
`rustc -Z ast-json` on an example of the structure and compare with the [nodes in the AST docs].
70+
`rustc -Z unpretty=ast-tree` on an example of the structure and compare with the [nodes in the AST docs].
7171
Usually the lint will end up to be a nested series of matches and ifs, [like so][deep-nesting].
72-
But we can make it nest-less by using [if_chain] macro, [like this][nest-less].
72+
But we can make it nest-less by using [let chains], [like this][nest-less].
7373

7474
[`E-medium`] issues are generally pretty easy too, though it's recommended you work on an [`good-first-issue`]
7575
first. Sometimes they are only somewhat involved code wise, but not difficult per-se.
@@ -87,9 +87,9 @@ an AST expression). `match_def_path()` in Clippy's `utils` module can also be us
8787
[`E-medium`]: https://github.com/rust-lang/rust-clippy/labels/E-medium
8888
[`ty`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty
8989
[nodes in the AST docs]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_ast/ast/
90-
[deep-nesting]: https://github.com/rust-lang/rust-clippy/blob/557f6848bd5b7183f55c1e1522a326e9e1df6030/clippy_lints/src/mem_forget.rs#L29-L43
91-
[if_chain]: https://docs.rs/if_chain/*/if_chain
92-
[nest-less]: https://github.com/rust-lang/rust-clippy/blob/557f6848bd5b7183f55c1e1522a326e9e1df6030/clippy_lints/src/bit_mask.rs#L124-L150
90+
[deep-nesting]: https://github.com/rust-lang/rust-clippy/blob/5e4f0922911536f80d9591180fa604229ac13939/clippy_lints/src/mem_forget.rs#L31-L45
91+
[let chains]: https://github.com/rust-lang/rust/pull/94927
92+
[nest-less]: https://github.com/rust-lang/rust-clippy/blob/5e4f0922911536f80d9591180fa604229ac13939/clippy_lints/src/bit_mask.rs#L133-L159
9393

9494
## Writing code
9595

src/tools/clippy/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "clippy"
3-
version = "0.1.62"
3+
version = "0.1.63"
44
description = "A bunch of helpful lints to avoid common pitfalls in Rust"
55
repository = "https://github.com/rust-lang/rust-clippy"
66
readme = "README.md"
@@ -25,6 +25,7 @@ clippy_lints = { path = "clippy_lints" }
2525
semver = "1.0"
2626
rustc_tools_util = { path = "rustc_tools_util" }
2727
tempfile = { version = "3.2", optional = true }
28+
termize = "0.1"
2829

2930
[dev-dependencies]
3031
compiletest_rs = { version = "0.7.1", features = ["tmp"] }

src/tools/clippy/clippy_dev/src/lint.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fn exit_if_err(status: io::Result<ExitStatus>) {
1313
}
1414
}
1515

16-
pub fn run(path: &str) {
16+
pub fn run<'a>(path: &str, args: impl Iterator<Item = &'a str>) {
1717
let is_file = match fs::metadata(path) {
1818
Ok(metadata) => metadata.is_file(),
1919
Err(e) => {
@@ -30,6 +30,7 @@ pub fn run(path: &str) {
3030
.args(["-Z", "no-codegen"])
3131
.args(["--edition", "2021"])
3232
.arg(path)
33+
.args(args)
3334
.status(),
3435
);
3536
} else {
@@ -42,6 +43,8 @@ pub fn run(path: &str) {
4243
.expect("failed to create tempdir");
4344

4445
let status = Command::new(cargo_clippy_path())
46+
.arg("clippy")
47+
.args(args)
4548
.current_dir(path)
4649
.env("CARGO_TARGET_DIR", target.as_ref())
4750
.status();

src/tools/clippy/clippy_dev/src/main.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ fn main() {
7676
},
7777
("lint", Some(matches)) => {
7878
let path = matches.value_of("path").unwrap();
79-
lint::run(path);
79+
let args = matches.values_of("args").into_iter().flatten();
80+
lint::run(path, args);
8081
},
8182
("rename_lint", Some(matches)) => {
8283
let old_name = matches.value_of("old_name").unwrap();
@@ -123,7 +124,7 @@ fn get_clap_config<'a>() -> ArgMatches<'a> {
123124
* the lint count in README.md is correct\n \
124125
* the changelog contains markdown link references at the bottom\n \
125126
* all lint groups include the correct lints\n \
126-
* lint modules in `clippy_lints/*` are visible in `src/lifb.rs` via `pub mod`\n \
127+
* lint modules in `clippy_lints/*` are visible in `src/lib.rs` via `pub mod`\n \
127128
* all lints are registered in the lint store",
128129
)
129130
.arg(Arg::with_name("print-only").long("print-only").help(
@@ -278,11 +279,23 @@ fn get_clap_config<'a>() -> ArgMatches<'a> {
278279
Lint a package directory:
279280
cargo dev lint tests/ui-cargo/wildcard_dependencies/fail
280281
cargo dev lint ~/my-project
282+
283+
Run rustfix:
284+
cargo dev lint ~/my-project -- --fix
285+
286+
Set lint levels:
287+
cargo dev lint file.rs -- -W clippy::pedantic
288+
cargo dev lint ~/my-project -- -- -W clippy::pedantic
281289
"})
282290
.arg(
283291
Arg::with_name("path")
284292
.required(true)
285293
.help("The path to a file or package directory to lint"),
294+
)
295+
.arg(
296+
Arg::with_name("args")
297+
.multiple(true)
298+
.help("Pass extra arguments to cargo/clippy-driver"),
286299
),
287300
)
288301
.subcommand(

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ fn to_camel_case(name: &str) -> String {
133133
.collect()
134134
}
135135

136-
fn get_stabilisation_version() -> String {
136+
fn get_stabilization_version() -> String {
137137
fn parse_manifest(contents: &str) -> Option<String> {
138138
let version = contents
139139
.lines()
@@ -199,7 +199,7 @@ fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String {
199199
},
200200
};
201201

202-
let version = get_stabilisation_version();
202+
let version = get_stabilization_version();
203203
let lint_name = lint.name;
204204
let category = lint.category;
205205
let name_camel = to_camel_case(lint.name);

src/tools/clippy/clippy_dev/src/update_lints.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const GENERATED_FILE_COMMENT: &str = "// This file was generated by `cargo dev u
1717

1818
const DOCS_LINK: &str = "https://rust-lang.github.io/rust-clippy/master/index.html";
1919

20-
#[derive(Clone, Copy, PartialEq)]
20+
#[derive(Clone, Copy, PartialEq, Eq)]
2121
pub enum UpdateMode {
2222
Check,
2323
Change,
@@ -66,8 +66,13 @@ fn generate_lint_files(
6666
|res| {
6767
for lint in usable_lints
6868
.iter()
69-
.map(|l| &l.name)
70-
.chain(deprecated_lints.iter().map(|l| &l.name))
69+
.map(|l| &*l.name)
70+
.chain(deprecated_lints.iter().map(|l| &*l.name))
71+
.chain(
72+
renamed_lints
73+
.iter()
74+
.map(|l| l.old_name.strip_prefix("clippy::").unwrap_or(&l.old_name)),
75+
)
7176
.sorted()
7277
{
7378
writeln!(res, "[`{}`]: {}#{}", lint, DOCS_LINK, lint).unwrap();
@@ -372,7 +377,7 @@ fn exit_with_failure() {
372377
}
373378

374379
/// Lint data parsed from the Clippy source code.
375-
#[derive(Clone, PartialEq, Debug)]
380+
#[derive(Clone, PartialEq, Eq, Debug)]
376381
struct Lint {
377382
name: String,
378383
group: String,
@@ -414,7 +419,7 @@ impl Lint {
414419
}
415420
}
416421

417-
#[derive(Clone, PartialEq, Debug)]
422+
#[derive(Clone, PartialEq, Eq, Debug)]
418423
struct DeprecatedLint {
419424
name: String,
420425
reason: String,

src/tools/clippy/clippy_lints/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "clippy_lints"
3-
version = "0.1.62"
3+
version = "0.1.63"
44
description = "A bunch of helpful lints to avoid common pitfalls in Rust"
55
repository = "https://github.com/rust-lang/rust-clippy"
66
readme = "README.md"

src/tools/clippy/clippy_lints/src/approx_const.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,7 @@ impl ApproxConstant {
8787
let s = s.as_str();
8888
if s.parse::<f64>().is_ok() {
8989
for &(constant, name, min_digits, msrv) in &KNOWN_CONSTS {
90-
if is_approx_const(constant, s, min_digits)
91-
&& msrv.as_ref().map_or(true, |msrv| meets_msrv(self.msrv.as_ref(), msrv))
92-
{
90+
if is_approx_const(constant, s, min_digits) && msrv.map_or(true, |msrv| meets_msrv(self.msrv, msrv)) {
9391
span_lint_and_help(
9492
cx,
9593
APPROX_CONSTANT,

src/tools/clippy/clippy_lints/src/assign_ops.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ declare_clippy_lint! {
5050
/// ### Known problems
5151
/// Clippy cannot know for sure if `a op= a op b` should have
5252
/// been `a = a op a op b` or `a = a op b`/`a op= b`. Therefore, it suggests both.
53-
/// If `a op= a op b` is really the correct behaviour it should be
53+
/// If `a op= a op b` is really the correct behavior it should be
5454
/// written as `a = a op a op b` as it's less confusing.
5555
///
5656
/// ### Example

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

+6-17
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use clippy_utils::msrvs;
66
use clippy_utils::source::{first_line_of_span, is_present_in_source, snippet_opt, without_block_comments};
77
use clippy_utils::{extract_msrv_attr, meets_msrv};
88
use if_chain::if_chain;
9-
use rustc_ast::{AttrKind, AttrStyle, Attribute, Lit, LitKind, MacArgs, MacArgsEq, MetaItemKind, NestedMetaItem};
9+
use rustc_ast::{AttrKind, AttrStyle, Attribute, Lit, LitKind, MetaItemKind, NestedMetaItem};
1010
use rustc_errors::Applicability;
1111
use rustc_hir::{
1212
Block, Expr, ExprKind, ImplItem, ImplItemKind, Item, ItemKind, StmtKind, TraitFn, TraitItem, TraitItemKind,
@@ -586,21 +586,10 @@ impl EarlyLintPass for EarlyAttributes {
586586

587587
fn check_empty_line_after_outer_attr(cx: &EarlyContext<'_>, item: &rustc_ast::Item) {
588588
for attr in &item.attrs {
589-
let attr_item = if let AttrKind::Normal(ref attr, _) = attr.kind {
590-
attr
591-
} else {
592-
return;
593-
};
594-
595-
if attr.style == AttrStyle::Outer {
596-
if let MacArgs::Eq(_, MacArgsEq::Ast(expr)) = &attr_item.args
597-
&& !matches!(expr.kind, rustc_ast::ExprKind::Lit(..)) {
598-
return;
599-
}
600-
if attr_item.args.inner_tokens().is_empty() || !is_present_in_source(cx, attr.span) {
601-
return;
602-
}
603-
589+
if matches!(attr.kind, AttrKind::Normal(..))
590+
&& attr.style == AttrStyle::Outer
591+
&& is_present_in_source(cx, attr.span)
592+
{
604593
let begin_of_attr_to_item = Span::new(attr.span.lo(), item.span.lo(), item.span.ctxt(), item.span.parent());
605594
let end_of_attr_to_item = Span::new(attr.span.hi(), item.span.lo(), item.span.ctxt(), item.span.parent());
606595

@@ -624,7 +613,7 @@ fn check_empty_line_after_outer_attr(cx: &EarlyContext<'_>, item: &rustc_ast::It
624613

625614
fn check_deprecated_cfg_attr(cx: &EarlyContext<'_>, attr: &Attribute, msrv: Option<RustcVersion>) {
626615
if_chain! {
627-
if meets_msrv(msrv.as_ref(), &msrvs::TOOL_ATTRIBUTES);
616+
if meets_msrv(msrv, msrvs::TOOL_ATTRIBUTES);
628617
// check cfg_attr
629618
if attr.has_name(sym::cfg_attr);
630619
if let Some(items) = attr.meta_item_list();

src/tools/clippy/clippy_lints/src/bit_mask.rs

+20-20
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use clippy_utils::consts::{constant, Constant};
22
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
33
use clippy_utils::sugg::Sugg;
4-
use if_chain::if_chain;
54
use rustc_ast::ast::LitKind;
65
use rustc_errors::Applicability;
76
use rustc_hir::{BinOpKind, Expr, ExprKind};
@@ -130,32 +129,33 @@ impl<'tcx> LateLintPass<'tcx> for BitMask {
130129
}
131130
}
132131
}
133-
if_chain! {
134-
if let ExprKind::Binary(op, left, right) = &e.kind;
135-
if BinOpKind::Eq == op.node;
136-
if let ExprKind::Binary(op1, left1, right1) = &left.kind;
137-
if BinOpKind::BitAnd == op1.node;
138-
if let ExprKind::Lit(lit) = &right1.kind;
139-
if let LitKind::Int(n, _) = lit.node;
140-
if let ExprKind::Lit(lit1) = &right.kind;
141-
if let LitKind::Int(0, _) = lit1.node;
142-
if n.leading_zeros() == n.count_zeros();
143-
if n > u128::from(self.verbose_bit_mask_threshold);
144-
then {
145-
span_lint_and_then(cx,
146-
VERBOSE_BIT_MASK,
147-
e.span,
148-
"bit mask could be simplified with a call to `trailing_zeros`",
149-
|diag| {
132+
133+
if let ExprKind::Binary(op, left, right) = &e.kind
134+
&& BinOpKind::Eq == op.node
135+
&& let ExprKind::Binary(op1, left1, right1) = &left.kind
136+
&& BinOpKind::BitAnd == op1.node
137+
&& let ExprKind::Lit(lit) = &right1.kind
138+
&& let LitKind::Int(n, _) = lit.node
139+
&& let ExprKind::Lit(lit1) = &right.kind
140+
&& let LitKind::Int(0, _) = lit1.node
141+
&& n.leading_zeros() == n.count_zeros()
142+
&& n > u128::from(self.verbose_bit_mask_threshold)
143+
{
144+
span_lint_and_then(
145+
cx,
146+
VERBOSE_BIT_MASK,
147+
e.span,
148+
"bit mask could be simplified with a call to `trailing_zeros`",
149+
|diag| {
150150
let sugg = Sugg::hir(cx, left1, "...").maybe_par();
151151
diag.span_suggestion(
152152
e.span,
153153
"try",
154154
format!("{}.trailing_zeros() >= {}", sugg, n.count_ones()),
155155
Applicability::MaybeIncorrect,
156156
);
157-
});
158-
}
157+
},
158+
);
159159
}
160160
}
161161
}

src/tools/clippy/clippy_lints/src/booleans.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ impl<'a, 'tcx, 'v> Hir2Qmm<'a, 'tcx, 'v> {
137137
}
138138
for (n, expr) in self.terminals.iter().enumerate() {
139139
if eq_expr_value(self.cx, e, expr) {
140-
#[allow(clippy::cast_possible_truncation)]
140+
#[expect(clippy::cast_possible_truncation)]
141141
return Ok(Bool::Term(n as u8));
142142
}
143143

@@ -149,15 +149,15 @@ impl<'a, 'tcx, 'v> Hir2Qmm<'a, 'tcx, 'v> {
149149
if eq_expr_value(self.cx, e_lhs, expr_lhs);
150150
if eq_expr_value(self.cx, e_rhs, expr_rhs);
151151
then {
152-
#[allow(clippy::cast_possible_truncation)]
152+
#[expect(clippy::cast_possible_truncation)]
153153
return Ok(Bool::Not(Box::new(Bool::Term(n as u8))));
154154
}
155155
}
156156
}
157157
let n = self.terminals.len();
158158
self.terminals.push(e);
159159
if n < 32 {
160-
#[allow(clippy::cast_possible_truncation)]
160+
#[expect(clippy::cast_possible_truncation)]
161161
Ok(Bool::Term(n as u8))
162162
} else {
163163
Err("too many literals".to_owned())

src/tools/clippy/clippy_lints/src/borrow_as_ptr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl BorrowAsPtr {
5757

5858
impl<'tcx> LateLintPass<'tcx> for BorrowAsPtr {
5959
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
60-
if !meets_msrv(self.msrv.as_ref(), &msrvs::BORROW_AS_PTR) {
60+
if !meets_msrv(self.msrv, msrvs::BORROW_AS_PTR) {
6161
return;
6262
}
6363

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ pub(super) fn check(
1616
cast_expr: &Expr<'_>,
1717
cast_from: Ty<'_>,
1818
cast_to: Ty<'_>,
19-
msrv: &Option<RustcVersion>,
19+
msrv: Option<RustcVersion>,
2020
) {
2121
if_chain! {
22-
if meets_msrv(msrv.as_ref(), &msrvs::UNSIGNED_ABS);
22+
if meets_msrv(msrv, msrvs::UNSIGNED_ABS);
2323
if cast_from.is_integral();
2424
if cast_to.is_integral();
2525
if cast_from.is_signed();

0 commit comments

Comments
 (0)