Skip to content

Commit 430d6ed

Browse files
committed
Auto merge of #144449 - flip1995:clippy-subtree-update, r=Manishearth
Clippy subtree update r? `@Manishearth`
2 parents a955f1c + 9660038 commit 430d6ed

File tree

167 files changed

+3791
-1125
lines changed

Some content is hidden

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

167 files changed

+3791
-1125
lines changed

src/bootstrap/src/core/build_steps/clippy.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const IGNORED_RULES_FOR_STD_AND_RUSTC: &[&str] = &[
1919
"too_many_arguments",
2020
"needless_lifetimes", // people want to keep the lifetimes
2121
"wrong_self_convention",
22+
"approx_constant", // libcore is what defines those
2223
];
2324

2425
fn lint_args(builder: &Builder<'_>, config: &LintConfig, ignored_rules: &[&str]) -> Vec<String> {

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

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,26 @@ jobs:
2020
# of the pull request, as malicious code would be able to access the private
2121
# GitHub token.
2222
steps:
23-
- name: Check PR Changes
24-
id: pr-changes
25-
run: echo "::set-output name=changes::${{ toJson(github.event.pull_request.changed_files) }}"
26-
27-
- name: Create Comment
28-
if: steps.pr-changes.outputs.changes != '[]'
29-
run: |
30-
# Use GitHub API to create a comment on the PR
31-
PR_NUMBER=${{ github.event.pull_request.number }}
32-
COMMENT="**Seems that you are trying to add a new lint!**\nWe are currently in a [feature freeze](https://doc.rust-lang.org/nightly/clippy/development/feature_freeze.html), so we are delaying all lint-adding PRs to September 18 and focusing on bugfixes.\nThanks a lot for your contribution, and sorry for the inconvenience.\nWith ❤ from the Clippy team\n\n@rustbot note Feature-freeze\n@rustbot blocked\n@rustbot label +A-lint\n"
33-
GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}
34-
COMMENT_URL="https://api.github.com/repos/${{ github.repository }}/issues/${PR_NUMBER}/comments"
35-
curl -s -H "Authorization: token ${GITHUB_TOKEN}" -X POST $COMMENT_URL -d "{\"body\":\"$COMMENT\"}"
23+
- name: Add freeze warning comment
24+
env:
25+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
26+
GITHUB_REPOSITORY: ${{ github.repository }}
27+
PR_NUMBER: ${{ github.event.pull_request.number }}
28+
run: |
29+
COMMENT=$(echo "**Seems that you are trying to add a new lint!**\n\
30+
\n\
31+
We are currently in a [feature freeze](https://doc.rust-lang.org/nightly/clippy/development/feature_freeze.html), so we are delaying all lint-adding PRs to September 18 and [focusing on bugfixes](https://github.com/rust-lang/rust-clippy/issues/15086).\n\
32+
\n\
33+
Thanks a lot for your contribution, and sorry for the inconvenience.\n\
34+
\n\
35+
With ❤ from the Clippy team.\n\
36+
\n\
37+
@rustbot note Feature-freeze\n\
38+
@rustbot blocked\n\
39+
@rustbot label +A-lint"
40+
)
41+
curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \
42+
-H "Content-Type: application/vnd.github.raw+json" \
43+
-X POST \
44+
--data "{\"body\":\"${COMMENT}\"}" \
45+
"https://api.github.com/repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/comments"

src/tools/clippy/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ out
1919

2020
# Generated by Cargo
2121
*Cargo.lock
22+
!/clippy_test_deps/Cargo.lock
2223
/target
2324
/clippy_lints/target
25+
/clippy_lints_internal/target
2426
/clippy_utils/target
2527
/clippy_dev/target
2628
/lintcheck/target

src/tools/clippy/clippy_dev/src/fmt.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::utils::{
33
walk_dir_no_dot_or_target,
44
};
55
use itertools::Itertools;
6-
use rustc_lexer::{TokenKind, tokenize};
6+
use rustc_lexer::{FrontmatterAllowed, TokenKind, tokenize};
77
use std::fmt::Write;
88
use std::fs;
99
use std::io::{self, Read};
@@ -92,7 +92,7 @@ fn fmt_conf(check: bool) -> Result<(), Error> {
9292
let mut fields = Vec::new();
9393
let mut state = State::Start;
9494

95-
for (i, t) in tokenize(conf)
95+
for (i, t) in tokenize(conf, FrontmatterAllowed::No)
9696
.map(|x| {
9797
let start = pos;
9898
pos += x.len;

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

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,11 @@ impl LateLintPass<'_> for ApproxConstant {
9292
impl ApproxConstant {
9393
fn check_known_consts(&self, cx: &LateContext<'_>, span: Span, s: symbol::Symbol, module: &str) {
9494
let s = s.as_str();
95-
if s.parse::<f64>().is_ok() {
95+
if let Ok(maybe_constant) = s.parse::<f64>() {
9696
for &(constant, name, min_digits, msrv) in &KNOWN_CONSTS {
97-
if is_approx_const(constant, s, min_digits) && msrv.is_none_or(|msrv| self.msrv.meets(cx, msrv)) {
97+
if is_approx_const(constant, s, maybe_constant, min_digits)
98+
&& msrv.is_none_or(|msrv| self.msrv.meets(cx, msrv))
99+
{
98100
span_lint_and_help(
99101
cx,
100102
APPROX_CONSTANT,
@@ -112,18 +114,35 @@ impl ApproxConstant {
112114

113115
impl_lint_pass!(ApproxConstant => [APPROX_CONSTANT]);
114116

117+
fn count_digits_after_dot(input: &str) -> usize {
118+
input
119+
.char_indices()
120+
.find(|(_, ch)| *ch == '.')
121+
.map_or(0, |(i, _)| input.len() - i - 1)
122+
}
123+
115124
/// Returns `false` if the number of significant figures in `value` are
116125
/// less than `min_digits`; otherwise, returns true if `value` is equal
117-
/// to `constant`, rounded to the number of digits present in `value`.
126+
/// to `constant`, rounded to the number of significant digits present in `value`.
118127
#[must_use]
119-
fn is_approx_const(constant: f64, value: &str, min_digits: usize) -> bool {
128+
fn is_approx_const(constant: f64, value: &str, f_value: f64, min_digits: usize) -> bool {
120129
if value.len() <= min_digits {
130+
// The value is not precise enough
121131
false
122-
} else if constant.to_string().starts_with(value) {
123-
// The value is a truncated constant
132+
} else if f_value.to_string().len() > min_digits && constant.to_string().starts_with(&f_value.to_string()) {
133+
// The value represents the same value
124134
true
125135
} else {
126-
let round_const = format!("{constant:.*}", value.len() - 2);
136+
// The value is a truncated constant
137+
138+
// Print constant with numeric formatting (`0`), with the length of `value` as minimum width
139+
// (`value_len$`), and with the same precision as `value` (`.value_prec$`).
140+
// See https://doc.rust-lang.org/std/fmt/index.html.
141+
let round_const = format!(
142+
"{constant:0value_len$.value_prec$}",
143+
value_len = value.len(),
144+
value_prec = count_digits_after_dot(value)
145+
);
127146
value == round_const
128147
}
129148
}

src/tools/clippy/clippy_lints/src/arbitrary_source_item_ordering.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ use clippy_utils::diagnostics::span_lint_and_note;
88
use clippy_utils::is_cfg_test;
99
use rustc_attr_data_structures::AttributeKind;
1010
use rustc_hir::{
11-
Attribute, FieldDef, HirId, IsAuto, ImplItemId, Item, ItemKind, Mod, OwnerId, QPath, TraitItemId, TyKind,
12-
Variant, VariantData,
11+
Attribute, FieldDef, HirId, ImplItemId, IsAuto, Item, ItemKind, Mod, OwnerId, QPath, TraitItemId, TyKind, Variant,
12+
VariantData,
1313
};
14-
use rustc_middle::ty::AssocKind;
1514
use rustc_lint::{LateContext, LateLintPass, LintContext};
15+
use rustc_middle::ty::AssocKind;
1616
use rustc_session::impl_lint_pass;
1717
use rustc_span::Ident;
1818

@@ -469,13 +469,14 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
469469
/// This is implemented here because `rustc_hir` is not a dependency of
470470
/// `clippy_config`.
471471
fn convert_assoc_item_kind(cx: &LateContext<'_>, owner_id: OwnerId) -> SourceItemOrderingTraitAssocItemKind {
472-
let kind = cx.tcx.associated_item(owner_id.def_id).kind;
473-
474472
#[allow(clippy::enum_glob_use)] // Very local glob use for legibility.
475473
use SourceItemOrderingTraitAssocItemKind::*;
474+
475+
let kind = cx.tcx.associated_item(owner_id.def_id).kind;
476+
476477
match kind {
477-
AssocKind::Const{..} => Const,
478-
AssocKind::Type {..}=> Type,
478+
AssocKind::Const { .. } => Const,
479+
AssocKind::Type { .. } => Type,
479480
AssocKind::Fn { .. } => Fn,
480481
}
481482
}

src/tools/clippy/clippy_lints/src/arc_with_non_send_sync.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ impl<'tcx> LateLintPass<'tcx> for ArcWithNonSendSync {
7373
diag.note(format!(
7474
"`Arc<{arg_ty}>` is not `Send` and `Sync` as `{arg_ty}` is {reason}"
7575
));
76-
diag.help("if the `Arc` will not used be across threads replace it with an `Rc`");
76+
diag.help("if the `Arc` will not be used across threads replace it with an `Rc`");
7777
diag.help(format!(
7878
"otherwise make `{arg_ty}` `Send` and `Sync` or consider a wrapper type such as `Mutex`"
7979
));

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ pub(super) fn check(cx: &EarlyContext<'_>, item: &Item, attrs: &[Attribute]) {
3636
| sym::unused_braces
3737
| sym::unused_import_braces
3838
| sym::unused_imports
39+
| sym::redundant_imports
3940
)
4041
{
4142
return;

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,8 @@ fn get_const_name_and_ty_name(
5959

6060
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>) {
6161
// We allow casts from any function type to any function type.
62-
match cast_to.kind() {
63-
ty::FnDef(..) | ty::FnPtr(..) => return,
64-
_ => { /* continue to checks */ },
62+
if cast_to.is_fn() {
63+
return;
6564
}
6665

6766
if let ty::FnDef(def_id, generics) = cast_from.kind()

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

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use clippy_utils::source::snippet_with_applicability;
33
use rustc_errors::Applicability;
44
use rustc_hir::Expr;
55
use rustc_lint::LateContext;
6-
use rustc_middle::ty::{self, Ty};
6+
use rustc_middle::ty::Ty;
77

88
use super::{FN_TO_NUMERIC_CAST, utils};
99

@@ -13,23 +13,20 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>,
1313
return;
1414
};
1515

16-
match cast_from.kind() {
17-
ty::FnDef(..) | ty::FnPtr(..) => {
18-
let mut applicability = Applicability::MaybeIncorrect;
16+
if cast_from.is_fn() {
17+
let mut applicability = Applicability::MaybeIncorrect;
1918

20-
if to_nbits >= cx.tcx.data_layout.pointer_size().bits() && !cast_to.is_usize() {
21-
let from_snippet = snippet_with_applicability(cx, cast_expr.span, "x", &mut applicability);
22-
span_lint_and_sugg(
23-
cx,
24-
FN_TO_NUMERIC_CAST,
25-
expr.span,
26-
format!("casting function pointer `{from_snippet}` to `{cast_to}`"),
27-
"try",
28-
format!("{from_snippet} as usize"),
29-
applicability,
30-
);
31-
}
32-
},
33-
_ => {},
19+
if to_nbits >= cx.tcx.data_layout.pointer_size().bits() && !cast_to.is_usize() {
20+
let from_snippet = snippet_with_applicability(cx, cast_expr.span, "x", &mut applicability);
21+
span_lint_and_sugg(
22+
cx,
23+
FN_TO_NUMERIC_CAST,
24+
expr.span,
25+
format!("casting function pointer `{from_snippet}` to `{cast_to}`"),
26+
"try",
27+
format!("{from_snippet} as usize"),
28+
applicability,
29+
);
30+
}
3431
}
3532
}

0 commit comments

Comments
 (0)