Skip to content

Commit d23062b

Browse files
committed
Auto merge of #116139 - flip1995:clippyup, r=Manishearth
Clippy subtree update r? `@Manishearth`
2 parents 27b4eb9 + 6d331b7 commit d23062b

File tree

198 files changed

+3995
-2324
lines changed

Some content is hidden

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

198 files changed

+3995
-2324
lines changed

Cargo.lock

-12
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,6 @@ version = "0.1.74"
512512
dependencies = [
513513
"clippy_lints",
514514
"clippy_utils",
515-
"derive-new",
516515
"filetime",
517516
"futures",
518517
"if_chain",
@@ -941,17 +940,6 @@ dependencies = [
941940
"syn 2.0.29",
942941
]
943942

944-
[[package]]
945-
name = "derive-new"
946-
version = "0.5.9"
947-
source = "registry+https://github.com/rust-lang/crates.io-index"
948-
checksum = "3418329ca0ad70234b9735dc4ceed10af4df60eff9c8e7b06cb5e520d92c3535"
949-
dependencies = [
950-
"proc-macro2",
951-
"quote",
952-
"syn 1.0.109",
953-
]
954-
955943
[[package]]
956944
name = "derive_builder"
957945
version = "0.12.0"

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,27 @@ jobs:
2121
- name: Setup Node.js
2222
uses: actions/setup-node@v3
2323
with:
24-
node-version: '14.x'
24+
node-version: '18.x'
2525

2626
- name: Install remark
2727
run: npm install remark-cli remark-lint remark-lint-maximum-line-length remark-preset-lint-recommended remark-gfm
2828

2929
- name: Install mdbook
3030
run: |
3131
mkdir mdbook
32-
curl -Lf https://github.com/rust-lang/mdBook/releases/download/v0.4.28/mdbook-v0.4.28-x86_64-unknown-linux-gnu.tar.gz | tar -xz --directory=./mdbook
32+
curl -Lf https://github.com/rust-lang/mdBook/releases/download/v0.4.34/mdbook-v0.4.34-x86_64-unknown-linux-gnu.tar.gz | tar -xz --directory=./mdbook
3333
echo `pwd`/mdbook >> $GITHUB_PATH
3434
3535
# Run
3636
- name: Check *.md files
37-
run: git ls-files -z '*.md' | xargs -0 -n 1 -I {} ./node_modules/.bin/remark {} -u lint -f > /dev/null
37+
run: ./node_modules/.bin/remark -u lint -f .
3838

3939
- name: Linkcheck book
4040
run: |
4141
rustup toolchain install nightly --component rust-docs
4242
curl https://raw.githubusercontent.com/rust-lang/rust/master/src/tools/linkchecker/linkcheck.sh -o linkcheck.sh
4343
sh linkcheck.sh clippy --path ./book
44-
44+
4545
- name: Build mdbook
4646
run: mdbook build book
4747

src/tools/clippy/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -5171,6 +5171,7 @@ Released 2018-09-13
51715171
[`needless_bool_assign`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_bool_assign
51725172
[`needless_borrow`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
51735173
[`needless_borrowed_reference`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrowed_reference
5174+
[`needless_borrows_for_generic_args`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrows_for_generic_args
51745175
[`needless_collect`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_collect
51755176
[`needless_continue`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_continue
51765177
[`needless_doctest_main`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_doctest_main
@@ -5245,6 +5246,7 @@ Released 2018-09-13
52455246
[`partialeq_ne_impl`]: https://rust-lang.github.io/rust-clippy/master/index.html#partialeq_ne_impl
52465247
[`partialeq_to_none`]: https://rust-lang.github.io/rust-clippy/master/index.html#partialeq_to_none
52475248
[`path_buf_push_overwrite`]: https://rust-lang.github.io/rust-clippy/master/index.html#path_buf_push_overwrite
5249+
[`path_ends_with_ext`]: https://rust-lang.github.io/rust-clippy/master/index.html#path_ends_with_ext
52485250
[`pattern_type_mismatch`]: https://rust-lang.github.io/rust-clippy/master/index.html#pattern_type_mismatch
52495251
[`permissions_set_readonly_false`]: https://rust-lang.github.io/rust-clippy/master/index.html#permissions_set_readonly_false
52505252
[`positional_named_format_parameters`]: https://rust-lang.github.io/rust-clippy/master/index.html#positional_named_format_parameters
@@ -5279,6 +5281,7 @@ Released 2018-09-13
52795281
[`readonly_write_lock`]: https://rust-lang.github.io/rust-clippy/master/index.html#readonly_write_lock
52805282
[`recursive_format_impl`]: https://rust-lang.github.io/rust-clippy/master/index.html#recursive_format_impl
52815283
[`redundant_allocation`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_allocation
5284+
[`redundant_as_str`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_as_str
52825285
[`redundant_async_block`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_async_block
52835286
[`redundant_at_rest_pattern`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_at_rest_pattern
52845287
[`redundant_clone`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_clone
@@ -5437,6 +5440,7 @@ Released 2018-09-13
54375440
[`unnecessary_join`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_join
54385441
[`unnecessary_lazy_evaluations`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_lazy_evaluations
54395442
[`unnecessary_literal_unwrap`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_literal_unwrap
5443+
[`unnecessary_map_on_constructor`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_map_on_constructor
54405444
[`unnecessary_mut_passed`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_mut_passed
54415445
[`unnecessary_operation`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_operation
54425446
[`unnecessary_owned_empty_strings`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_owned_empty_strings
@@ -5574,5 +5578,6 @@ Released 2018-09-13
55745578
[`allow-one-hash-in-raw-strings`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-one-hash-in-raw-strings
55755579
[`absolute-paths-max-segments`]: https://doc.rust-lang.org/clippy/lint_configuration.html#absolute-paths-max-segments
55765580
[`absolute-paths-allowed-crates`]: https://doc.rust-lang.org/clippy/lint_configuration.html#absolute-paths-allowed-crates
5581+
[`allowed-dotfiles`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allowed-dotfiles
55775582
[`enforce-iter-loop-reborrow`]: https://doc.rust-lang.org/clippy/lint_configuration.html#enforce-iter-loop-reborrow
55785583
<!-- end autogenerated links to configuration documentation -->

src/tools/clippy/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ itertools = "0.10.1"
3838

3939
# UI test dependencies
4040
clippy_utils = { path = "clippy_utils" }
41-
derive-new = "0.5"
4241
if_chain = "1.0"
4342
quote = "1.0"
4443
serde = { version = "1.0.125", features = ["derive"] }

src/tools/clippy/book/src/lint_configuration.md

+12-2
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ Minimum chars an ident can have, anything below or equal to this will be linted.
703703
## `accept-comment-above-statement`
704704
Whether to accept a safety comment to be placed above the statement containing the `unsafe` block
705705

706-
**Default Value:** `false` (`bool`)
706+
**Default Value:** `true` (`bool`)
707707

708708
---
709709
**Affected lints:**
@@ -713,7 +713,7 @@ Whether to accept a safety comment to be placed above the statement containing t
713713
## `accept-comment-above-attributes`
714714
Whether to accept a safety comment to be placed above the attributes for the `unsafe` block
715715

716-
**Default Value:** `false` (`bool`)
716+
**Default Value:** `true` (`bool`)
717717

718718
---
719719
**Affected lints:**
@@ -751,6 +751,16 @@ Which crates to allow absolute paths from
751751
* [`absolute_paths`](https://rust-lang.github.io/rust-clippy/master/index.html#absolute_paths)
752752

753753

754+
## `allowed-dotfiles`
755+
Additional dotfiles (files or directories starting with a dot) to allow
756+
757+
**Default Value:** `{}` (`rustc_data_structures::fx::FxHashSet<String>`)
758+
759+
---
760+
**Affected lints:**
761+
* [`path_ends_with_ext`](https://rust-lang.github.io/rust-clippy/master/index.html#path_ends_with_ext)
762+
763+
754764
## `enforce-iter-loop-reborrow`
755765
#### Example
756766
```

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ fn check_should_panic_reason(cx: &LateContext<'_>, attr: &Attribute) {
616616
attr.span,
617617
"#[should_panic] attribute without a reason",
618618
"consider specifying the expected panic",
619-
r#"#[should_panic(expected = /* panic message */)]"#.into(),
619+
"#[should_panic(expected = /* panic message */)]".into(),
620620
Applicability::HasPlaceholders,
621621
);
622622
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub(super) fn check(
2525
// The suggestion is to use a function call, so if the original expression
2626
// has parens on the outside, they are no longer needed.
2727
let mut applicability = Applicability::MachineApplicable;
28-
let opt = snippet_opt(cx, cast_op.span);
28+
let opt = snippet_opt(cx, cast_op.span.source_callsite());
2929
let sugg = opt.as_ref().map_or_else(
3030
|| {
3131
applicability = Applicability::HasPlaceholders;

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ fn apply_reductions(cx: &LateContext<'_>, nbits: u64, expr: &Expr<'_>, signed: b
4444
.unwrap_or(u64::max_value())
4545
.min(apply_reductions(cx, nbits, left, signed)),
4646
BinOpKind::Shr => apply_reductions(cx, nbits, left, signed)
47-
.saturating_sub(constant_int(cx, right).map_or(0, |s| u64::try_from(s).expect("shift too high"))),
47+
.saturating_sub(constant_int(cx, right).map_or(0, |s| u64::try_from(s).unwrap_or_default())),
4848
_ => nbits,
4949
},
5050
ExprKind::MethodCall(method, left, [right], _) => {

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

+26
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ mod ptr_as_ptr;
2020
mod ptr_cast_constness;
2121
mod unnecessary_cast;
2222
mod utils;
23+
mod zero_ptr;
2324

2425
use clippy_utils::is_hir_ty_cfg_dependant;
2526
use clippy_utils::msrvs::{self, Msrv};
@@ -665,6 +666,29 @@ declare_clippy_lint! {
665666
"casting a known floating-point NaN into an integer"
666667
}
667668

669+
declare_clippy_lint! {
670+
/// ### What it does
671+
/// Catch casts from `0` to some pointer type
672+
///
673+
/// ### Why is this bad?
674+
/// This generally means `null` and is better expressed as
675+
/// {`std`, `core`}`::ptr::`{`null`, `null_mut`}.
676+
///
677+
/// ### Example
678+
/// ```rust
679+
/// let a = 0 as *const u32;
680+
/// ```
681+
///
682+
/// Use instead:
683+
/// ```rust
684+
/// let a = std::ptr::null::<u32>();
685+
/// ```
686+
#[clippy::version = "pre 1.29.0"]
687+
pub ZERO_PTR,
688+
style,
689+
"using `0 as *{const, mut} T`"
690+
}
691+
668692
pub struct Casts {
669693
msrv: Msrv,
670694
}
@@ -699,6 +723,7 @@ impl_lint_pass!(Casts => [
699723
CAST_SLICE_FROM_RAW_PARTS,
700724
AS_PTR_CAST_MUT,
701725
CAST_NAN_TO_INT,
726+
ZERO_PTR,
702727
]);
703728

704729
impl<'tcx> LateLintPass<'tcx> for Casts {
@@ -729,6 +754,7 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
729754
fn_to_numeric_cast_any::check(cx, expr, cast_expr, cast_from, cast_to);
730755
fn_to_numeric_cast::check(cx, expr, cast_expr, cast_from, cast_to);
731756
fn_to_numeric_cast_with_truncation::check(cx, expr, cast_expr, cast_from, cast_to);
757+
zero_ptr::check(cx, expr, cast_expr, cast_to_hir);
732758

733759
if cast_to.is_numeric() && !in_external_macro(cx.sess(), expr.span) {
734760
cast_possible_truncation::check(cx, expr, cast_expr, cast_from, cast_to, cast_to_hir.span);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::source::snippet_opt;
3+
use clippy_utils::{in_constant, is_integer_literal, std_or_core};
4+
use rustc_errors::Applicability;
5+
use rustc_hir::{Expr, Mutability, Ty, TyKind};
6+
use rustc_lint::LateContext;
7+
8+
use super::ZERO_PTR;
9+
10+
pub fn check(cx: &LateContext<'_>, expr: &Expr<'_>, from: &Expr<'_>, to: &Ty<'_>) {
11+
if let TyKind::Ptr(ref mut_ty) = to.kind
12+
&& is_integer_literal(from, 0)
13+
&& !in_constant(cx, from.hir_id)
14+
&& let Some(std_or_core) = std_or_core(cx)
15+
{
16+
let (msg, sugg_fn) = match mut_ty.mutbl {
17+
Mutability::Mut => ("`0 as *mut _` detected", "ptr::null_mut"),
18+
Mutability::Not => ("`0 as *const _` detected", "ptr::null"),
19+
};
20+
21+
let sugg = if let TyKind::Infer = mut_ty.ty.kind {
22+
format!("{std_or_core}::{sugg_fn}()")
23+
} else if let Some(mut_ty_snip) = snippet_opt(cx, mut_ty.ty.span) {
24+
format!("{std_or_core}::{sugg_fn}::<{mut_ty_snip}>()")
25+
} else {
26+
return;
27+
};
28+
29+
span_lint_and_sugg(
30+
cx,
31+
ZERO_PTR,
32+
expr.span,
33+
msg,
34+
"try",
35+
sugg,
36+
Applicability::MachineApplicable,
37+
);
38+
}
39+
}

src/tools/clippy/clippy_lints/src/declared_lints.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
9797
crate::casts::PTR_AS_PTR_INFO,
9898
crate::casts::PTR_CAST_CONSTNESS_INFO,
9999
crate::casts::UNNECESSARY_CAST_INFO,
100+
crate::casts::ZERO_PTR_INFO,
100101
crate::checked_conversions::CHECKED_CONVERSIONS_INFO,
101102
crate::cognitive_complexity::COGNITIVE_COMPLEXITY_INFO,
102103
crate::collapsible_if::COLLAPSIBLE_ELSE_IF_INFO,
@@ -399,9 +400,11 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
399400
crate::methods::OR_FUN_CALL_INFO,
400401
crate::methods::OR_THEN_UNWRAP_INFO,
401402
crate::methods::PATH_BUF_PUSH_OVERWRITE_INFO,
403+
crate::methods::PATH_ENDS_WITH_EXT_INFO,
402404
crate::methods::RANGE_ZIP_WITH_LEN_INFO,
403405
crate::methods::READONLY_WRITE_LOCK_INFO,
404406
crate::methods::READ_LINE_WITHOUT_TRIM_INFO,
407+
crate::methods::REDUNDANT_AS_STR_INFO,
405408
crate::methods::REPEAT_ONCE_INFO,
406409
crate::methods::RESULT_MAP_OR_INTO_OPTION_INFO,
407410
crate::methods::SEARCH_IS_SOME_INFO,
@@ -441,7 +444,6 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
441444
crate::misc::SHORT_CIRCUIT_STATEMENT_INFO,
442445
crate::misc::TOPLEVEL_REF_ARG_INFO,
443446
crate::misc::USED_UNDERSCORE_BINDING_INFO,
444-
crate::misc::ZERO_PTR_INFO,
445447
crate::misc_early::BUILTIN_TYPE_SHADOW_INFO,
446448
crate::misc_early::DOUBLE_NEG_INFO,
447449
crate::misc_early::DUPLICATE_UNDERSCORE_ARGUMENT_INFO,
@@ -479,6 +481,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
479481
crate::needless_bool::NEEDLESS_BOOL_INFO,
480482
crate::needless_bool::NEEDLESS_BOOL_ASSIGN_INFO,
481483
crate::needless_borrowed_ref::NEEDLESS_BORROWED_REFERENCE_INFO,
484+
crate::needless_borrows_for_generic_args::NEEDLESS_BORROWS_FOR_GENERIC_ARGS_INFO,
482485
crate::needless_continue::NEEDLESS_CONTINUE_INFO,
483486
crate::needless_else::NEEDLESS_ELSE_INFO,
484487
crate::needless_for_each::NEEDLESS_FOR_EACH_INFO,
@@ -671,6 +674,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
671674
crate::unnamed_address::FN_ADDRESS_COMPARISONS_INFO,
672675
crate::unnamed_address::VTABLE_ADDRESS_COMPARISONS_INFO,
673676
crate::unnecessary_box_returns::UNNECESSARY_BOX_RETURNS_INFO,
677+
crate::unnecessary_map_on_constructor::UNNECESSARY_MAP_ON_CONSTRUCTOR_INFO,
674678
crate::unnecessary_owned_empty_strings::UNNECESSARY_OWNED_EMPTY_STRINGS_INFO,
675679
crate::unnecessary_self_imports::UNNECESSARY_SELF_IMPORTS_INFO,
676680
crate::unnecessary_struct_initialization::UNNECESSARY_STRUCT_INITIALIZATION_INFO,

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

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
2-
use rustc_hir::{self as hir, HirId, Item, ItemKind};
3-
use rustc_hir_analysis::hir_ty_to_ty;
2+
use rustc_hir::{HirId, Item, ItemKind};
43
use rustc_lint::{LateContext, LateLintPass};
54
use rustc_middle::ty::layout::LayoutOf;
5+
use rustc_middle::ty::{self, FieldDef, GenericArg, List};
66
use rustc_session::{declare_lint_pass, declare_tool_lint};
77
use rustc_span::sym;
88

@@ -52,7 +52,10 @@ declare_lint_pass!(DefaultUnionRepresentation => [DEFAULT_UNION_REPRESENTATION])
5252

5353
impl<'tcx> LateLintPass<'tcx> for DefaultUnionRepresentation {
5454
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
55-
if is_union_with_two_non_zst_fields(cx, item) && !has_c_repr_attr(cx, item.hir_id()) {
55+
if !item.span.from_expansion()
56+
&& is_union_with_two_non_zst_fields(cx, item)
57+
&& !has_c_repr_attr(cx, item.hir_id())
58+
{
5659
span_lint_and_help(
5760
cx,
5861
DEFAULT_UNION_REPRESENTATION,
@@ -73,18 +76,17 @@ impl<'tcx> LateLintPass<'tcx> for DefaultUnionRepresentation {
7376
/// if there is only one field left after ignoring ZST fields then the offset
7477
/// of that field does not matter either.)
7578
fn is_union_with_two_non_zst_fields(cx: &LateContext<'_>, item: &Item<'_>) -> bool {
76-
if let ItemKind::Union(data, _) = &item.kind {
77-
data.fields().iter().filter(|f| !is_zst(cx, f.ty)).count() >= 2
79+
if let ItemKind::Union(..) = &item.kind
80+
&& let ty::Adt(adt_def, args) = cx.tcx.type_of(item.owner_id).instantiate_identity().kind()
81+
{
82+
adt_def.all_fields().filter(|f| !is_zst(cx, f, args)).count() >= 2
7883
} else {
7984
false
8085
}
8186
}
8287

83-
fn is_zst(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>) -> bool {
84-
if hir_ty.span.from_expansion() {
85-
return false;
86-
}
87-
let ty = hir_ty_to_ty(cx.tcx, hir_ty);
88+
fn is_zst<'tcx>(cx: &LateContext<'tcx>, field: &FieldDef, args: &'tcx List<GenericArg<'tcx>>) -> bool {
89+
let ty = field.ty(cx.tcx, args);
8890
if let Ok(layout) = cx.layout_of(ty) {
8991
layout.is_zst()
9092
} else {

0 commit comments

Comments
 (0)