Skip to content

Commit 6e80599

Browse files
authored
Merge branch 'rust-lang:master' into master
2 parents e47f493 + f2922e7 commit 6e80599

File tree

156 files changed

+3165
-578
lines changed

Some content is hidden

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

156 files changed

+3165
-578
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5685,6 +5685,7 @@ Released 2018-09-13
56855685
[`cmp_nan`]: https://rust-lang.github.io/rust-clippy/master/index.html#cmp_nan
56865686
[`cmp_null`]: https://rust-lang.github.io/rust-clippy/master/index.html#cmp_null
56875687
[`cmp_owned`]: https://rust-lang.github.io/rust-clippy/master/index.html#cmp_owned
5688+
[`coerce_container_to_any`]: https://rust-lang.github.io/rust-clippy/master/index.html#coerce_container_to_any
56885689
[`cognitive_complexity`]: https://rust-lang.github.io/rust-clippy/master/index.html#cognitive_complexity
56895690
[`collapsible_else_if`]: https://rust-lang.github.io/rust-clippy/master/index.html#collapsible_else_if
56905691
[`collapsible_if`]: https://rust-lang.github.io/rust-clippy/master/index.html#collapsible_if
@@ -5736,6 +5737,7 @@ Released 2018-09-13
57365737
[`doc_markdown`]: https://rust-lang.github.io/rust-clippy/master/index.html#doc_markdown
57375738
[`doc_nested_refdefs`]: https://rust-lang.github.io/rust-clippy/master/index.html#doc_nested_refdefs
57385739
[`doc_overindented_list_items`]: https://rust-lang.github.io/rust-clippy/master/index.html#doc_overindented_list_items
5740+
[`doc_suspicious_footnotes`]: https://rust-lang.github.io/rust-clippy/master/index.html#doc_suspicious_footnotes
57395741
[`double_comparisons`]: https://rust-lang.github.io/rust-clippy/master/index.html#double_comparisons
57405742
[`double_ended_iterator_last`]: https://rust-lang.github.io/rust-clippy/master/index.html#double_ended_iterator_last
57415743
[`double_must_use`]: https://rust-lang.github.io/rust-clippy/master/index.html#double_must_use
@@ -5862,6 +5864,7 @@ Released 2018-09-13
58625864
[`ineffective_open_options`]: https://rust-lang.github.io/rust-clippy/master/index.html#ineffective_open_options
58635865
[`inefficient_to_string`]: https://rust-lang.github.io/rust-clippy/master/index.html#inefficient_to_string
58645866
[`infallible_destructuring_match`]: https://rust-lang.github.io/rust-clippy/master/index.html#infallible_destructuring_match
5867+
[`infallible_try_from`]: https://rust-lang.github.io/rust-clippy/master/index.html#infallible_try_from
58655868
[`infinite_iter`]: https://rust-lang.github.io/rust-clippy/master/index.html#infinite_iter
58665869
[`infinite_loop`]: https://rust-lang.github.io/rust-clippy/master/index.html#infinite_loop
58675870
[`inherent_to_string`]: https://rust-lang.github.io/rust-clippy/master/index.html#inherent_to_string
@@ -5888,6 +5891,7 @@ Released 2018-09-13
58885891
[`inverted_saturating_sub`]: https://rust-lang.github.io/rust-clippy/master/index.html#inverted_saturating_sub
58895892
[`invisible_characters`]: https://rust-lang.github.io/rust-clippy/master/index.html#invisible_characters
58905893
[`io_other_error`]: https://rust-lang.github.io/rust-clippy/master/index.html#io_other_error
5894+
[`ip_constant`]: https://rust-lang.github.io/rust-clippy/master/index.html#ip_constant
58915895
[`is_digit_ascii_radix`]: https://rust-lang.github.io/rust-clippy/master/index.html#is_digit_ascii_radix
58925896
[`items_after_statements`]: https://rust-lang.github.io/rust-clippy/master/index.html#items_after_statements
58935897
[`items_after_test_module`]: https://rust-lang.github.io/rust-clippy/master/index.html#items_after_test_module
@@ -6163,6 +6167,7 @@ Released 2018-09-13
61636167
[`pathbuf_init_then_push`]: https://rust-lang.github.io/rust-clippy/master/index.html#pathbuf_init_then_push
61646168
[`pattern_type_mismatch`]: https://rust-lang.github.io/rust-clippy/master/index.html#pattern_type_mismatch
61656169
[`permissions_set_readonly_false`]: https://rust-lang.github.io/rust-clippy/master/index.html#permissions_set_readonly_false
6170+
[`pointer_format`]: https://rust-lang.github.io/rust-clippy/master/index.html#pointer_format
61666171
[`pointers_in_nomem_asm_block`]: https://rust-lang.github.io/rust-clippy/master/index.html#pointers_in_nomem_asm_block
61676172
[`positional_named_format_parameters`]: https://rust-lang.github.io/rust-clippy/master/index.html#positional_named_format_parameters
61686173
[`possible_missing_comma`]: https://rust-lang.github.io/rust-clippy/master/index.html#possible_missing_comma

book/src/development/trait_checking.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ providing the `LateContext` (`cx`), our expression at hand, and
1717
the symbol of the trait in question:
1818

1919
```rust
20-
use clippy_utils::is_trait_method;
20+
use clippy_utils::ty::implements_trait;
2121
use rustc_hir::Expr;
2222
use rustc_lint::{LateContext, LateLintPass};
2323
use rustc_span::symbol::sym;

clippy_dev/src/update_lints.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ pub fn generate_lint_files(
7373
(
7474
"clippy_lints/src/lib.rs",
7575
&mut update_text_region_fn(
76-
"// begin lints modules, do not remove this comment, its used in `update_lints`\n",
77-
"// end lints modules, do not remove this comment, its used in `update_lints`",
76+
"// begin lints modules, do not remove this comment, it's used in `update_lints`\n",
77+
"// end lints modules, do not remove this comment, it's used in `update_lints`",
7878
|dst| {
7979
for lint_mod in lints.iter().map(|l| &l.module).sorted().dedup() {
8080
writeln!(dst, "mod {lint_mod};").unwrap();

clippy_lints/src/arbitrary_source_item_ordering.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ impl ArbitrarySourceItemOrdering {
204204
self.assoc_types_order
205205
),
206206
Some(before_item.span),
207-
format!("should be placed before `{}`", before_item.ident.as_str(),),
207+
format!("should be placed before `{}`", before_item.ident.name),
208208
);
209209
}
210210

@@ -216,7 +216,7 @@ impl ArbitrarySourceItemOrdering {
216216
ident.span,
217217
"incorrect ordering of items (must be alphabetically ordered)",
218218
Some(before_ident.span),
219-
format!("should be placed before `{}`", before_ident.as_str(),),
219+
format!("should be placed before `{}`", before_ident.name),
220220
);
221221
}
222222

@@ -228,7 +228,7 @@ impl ArbitrarySourceItemOrdering {
228228
};
229229

230230
let (before_span, note) = if let Some(ident) = before_item.kind.ident() {
231-
(ident.span, format!("should be placed before `{}`", ident.as_str(),))
231+
(ident.span, format!("should be placed before `{}`", ident.name))
232232
} else {
233233
(
234234
before_item.span,
@@ -255,7 +255,7 @@ impl ArbitrarySourceItemOrdering {
255255
self.assoc_types_order
256256
),
257257
Some(before_item.span),
258-
format!("should be placed before `{}`", before_item.ident.as_str(),),
258+
format!("should be placed before `{}`", before_item.ident.name),
259259
);
260260
}
261261
}

clippy_lints/src/arc_with_non_send_sync.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl<'tcx> LateLintPass<'tcx> for ArcWithNonSendSync {
5050
&& let arg_ty = cx.typeck_results().expr_ty(arg)
5151
// make sure that the type is not and does not contain any type parameters
5252
&& arg_ty.walk().all(|arg| {
53-
!matches!(arg.unpack(), GenericArgKind::Type(ty) if matches!(ty.kind(), ty::Param(_)))
53+
!matches!(arg.kind(), GenericArgKind::Type(ty) if matches!(ty.kind(), ty::Param(_)))
5454
})
5555
&& let Some(send) = cx.tcx.get_diagnostic_item(sym::Send)
5656
&& let Some(sync) = cx.tcx.lang_items().sync_trait()

clippy_lints/src/attrs/deprecated_cfg_attr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub(super) fn check_clippy(cx: &EarlyContext<'_>, attr: &Attribute) {
6161

6262
fn check_deprecated_cfg_recursively(cx: &EarlyContext<'_>, attr: &rustc_ast::MetaItem) {
6363
if let Some(ident) = attr.ident() {
64-
if ["any", "all", "not"].contains(&ident.name.as_str()) {
64+
if matches!(ident.name, sym::any | sym::all | sym::not) {
6565
let Some(list) = attr.meta_item_list() else { return };
6666
for item in list.iter().filter_map(|item| item.meta_item()) {
6767
check_deprecated_cfg_recursively(cx, item);

clippy_lints/src/attrs/duplicated_attributes.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use super::DUPLICATED_ATTRIBUTES;
22
use clippy_utils::diagnostics::span_lint_and_then;
3+
use itertools::Itertools;
34
use rustc_ast::{Attribute, MetaItem};
45
use rustc_data_structures::fx::FxHashMap;
56
use rustc_lint::EarlyContext;
6-
use rustc_span::{Span, sym};
7+
use rustc_span::{Span, Symbol, sym};
78
use std::collections::hash_map::Entry;
89

910
fn emit_if_duplicated(
@@ -29,7 +30,7 @@ fn check_duplicated_attr(
2930
cx: &EarlyContext<'_>,
3031
attr: &MetaItem,
3132
attr_paths: &mut FxHashMap<String, Span>,
32-
parent: &mut Vec<String>,
33+
parent: &mut Vec<Symbol>,
3334
) {
3435
if attr.span.from_expansion() {
3536
return;
@@ -43,25 +44,30 @@ fn check_duplicated_attr(
4344
return;
4445
}
4546
if let Some(direct_parent) = parent.last()
46-
&& direct_parent == sym::cfg_trace.as_str()
47+
&& *direct_parent == sym::cfg_trace
4748
&& [sym::all, sym::not, sym::any].contains(&name)
4849
{
4950
// FIXME: We don't correctly check `cfg`s for now, so if it's more complex than just a one
5051
// level `cfg`, we leave.
5152
return;
5253
}
5354
if let Some(value) = attr.value_str() {
54-
emit_if_duplicated(cx, attr, attr_paths, format!("{}:{name}={value}", parent.join(":")));
55+
emit_if_duplicated(
56+
cx,
57+
attr,
58+
attr_paths,
59+
format!("{}:{name}={value}", parent.iter().join(":")),
60+
);
5561
} else if let Some(sub_attrs) = attr.meta_item_list() {
56-
parent.push(name.as_str().to_string());
62+
parent.push(name);
5763
for sub_attr in sub_attrs {
5864
if let Some(meta) = sub_attr.meta_item() {
5965
check_duplicated_attr(cx, meta, attr_paths, parent);
6066
}
6167
}
6268
parent.pop();
6369
} else {
64-
emit_if_duplicated(cx, attr, attr_paths, format!("{}:{name}", parent.join(":")));
70+
emit_if_duplicated(cx, attr, attr_paths, format!("{}:{name}", parent.iter().join(":")));
6571
}
6672
}
6773

clippy_lints/src/booleans.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use clippy_config::Conf;
22
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_hir_and_then};
3-
use clippy_utils::eq_expr_value;
43
use clippy_utils::msrvs::{self, Msrv};
54
use clippy_utils::source::SpanRangeExt;
65
use clippy_utils::sugg::Sugg;
76
use clippy_utils::ty::{implements_trait, is_type_diagnostic_item};
7+
use clippy_utils::{eq_expr_value, sym};
88
use rustc_ast::ast::LitKind;
99
use rustc_attr_data_structures::RustcVersion;
1010
use rustc_errors::Applicability;
@@ -13,7 +13,7 @@ use rustc_hir::{BinOpKind, Body, Expr, ExprKind, FnDecl, UnOp};
1313
use rustc_lint::{LateContext, LateLintPass, Level};
1414
use rustc_session::impl_lint_pass;
1515
use rustc_span::def_id::LocalDefId;
16-
use rustc_span::{Span, SyntaxContext, sym};
16+
use rustc_span::{Span, Symbol, SyntaxContext};
1717

1818
declare_clippy_lint! {
1919
/// ### What it does
@@ -73,10 +73,10 @@ declare_clippy_lint! {
7373
}
7474

7575
// For each pairs, both orders are considered.
76-
const METHODS_WITH_NEGATION: [(Option<RustcVersion>, &str, &str); 3] = [
77-
(None, "is_some", "is_none"),
78-
(None, "is_err", "is_ok"),
79-
(Some(msrvs::IS_NONE_OR), "is_some_and", "is_none_or"),
76+
const METHODS_WITH_NEGATION: [(Option<RustcVersion>, Symbol, Symbol); 3] = [
77+
(None, sym::is_some, sym::is_none),
78+
(None, sym::is_err, sym::is_ok),
79+
(Some(msrvs::IS_NONE_OR), sym::is_some_and, sym::is_none_or),
8080
];
8181

8282
pub struct NonminimalBool {
@@ -440,9 +440,7 @@ fn simplify_not(cx: &LateContext<'_>, curr_msrv: Msrv, expr: &Expr<'_>) -> Optio
440440
.iter()
441441
.copied()
442442
.flat_map(|(msrv, a, b)| vec![(msrv, a, b), (msrv, b, a)])
443-
.find(|&(msrv, a, _)| {
444-
a == path.ident.name.as_str() && msrv.is_none_or(|msrv| curr_msrv.meets(cx, msrv))
445-
})
443+
.find(|&(msrv, a, _)| a == path.ident.name && msrv.is_none_or(|msrv| curr_msrv.meets(cx, msrv)))
446444
.and_then(|(_, _, neg_method)| {
447445
let negated_args = args
448446
.iter()

clippy_lints/src/casts/cast_lossless.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,29 +76,28 @@ fn should_lint(cx: &LateContext<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>, msrv: M
7676
return false;
7777
}
7878

79-
match (cast_from.is_integral(), cast_to.is_integral()) {
80-
(true, true) => {
79+
match (
80+
utils::int_ty_to_nbits(cx.tcx, cast_from),
81+
utils::int_ty_to_nbits(cx.tcx, cast_to),
82+
) {
83+
(Some(from_nbits), Some(to_nbits)) => {
8184
let cast_signed_to_unsigned = cast_from.is_signed() && !cast_to.is_signed();
82-
let from_nbits = utils::int_ty_to_nbits(cast_from, cx.tcx);
83-
let to_nbits = utils::int_ty_to_nbits(cast_to, cx.tcx);
8485
!is_isize_or_usize(cast_from)
8586
&& !is_isize_or_usize(cast_to)
8687
&& from_nbits < to_nbits
8788
&& !cast_signed_to_unsigned
8889
},
8990

90-
(true, false) => {
91-
let from_nbits = utils::int_ty_to_nbits(cast_from, cx.tcx);
91+
(Some(from_nbits), None) => {
92+
// FIXME: handle `f16` and `f128`
9293
let to_nbits = if let ty::Float(FloatTy::F32) = cast_to.kind() {
9394
32
9495
} else {
9596
64
9697
};
9798
!is_isize_or_usize(cast_from) && from_nbits < to_nbits
9899
},
99-
(false, true) if matches!(cast_from.kind(), ty::Bool) && msrv.meets(cx, msrvs::FROM_BOOL) => true,
100-
(_, _) => {
101-
matches!(cast_from.kind(), ty::Float(FloatTy::F32)) && matches!(cast_to.kind(), ty::Float(FloatTy::F64))
102-
},
100+
(None, Some(_)) if cast_from.is_bool() && msrv.meets(cx, msrvs::FROM_BOOL) => true,
101+
_ => matches!(cast_from.kind(), ty::Float(FloatTy::F32)) && matches!(cast_to.kind(), ty::Float(FloatTy::F64)),
103102
}
104103
}

clippy_lints/src/casts/cast_possible_truncation.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,14 @@ pub(super) fn check(
9191
cast_to: Ty<'_>,
9292
cast_to_span: Span,
9393
) {
94-
let msg = match (cast_from.kind(), cast_to.is_integral()) {
95-
(ty::Int(_) | ty::Uint(_), true) => {
94+
let msg = match (cast_from.kind(), utils::int_ty_to_nbits(cx.tcx, cast_to)) {
95+
(ty::Int(_) | ty::Uint(_), Some(to_nbits)) => {
9696
let from_nbits = apply_reductions(
9797
cx,
98-
utils::int_ty_to_nbits(cast_from, cx.tcx),
98+
utils::int_ty_to_nbits(cx.tcx, cast_from).unwrap(),
9999
cast_expr,
100100
cast_from.is_signed(),
101101
);
102-
let to_nbits = utils::int_ty_to_nbits(cast_to, cx.tcx);
103102

104103
let (should_lint, suffix) = match (is_isize_or_usize(cast_from), is_isize_or_usize(cast_to)) {
105104
(true, true) | (false, false) => (to_nbits < from_nbits, ""),
@@ -121,7 +120,7 @@ pub(super) fn check(
121120
format!("casting `{cast_from}` to `{cast_to}` may truncate the value{suffix}",)
122121
},
123122

124-
(ty::Adt(def, _), true) if def.is_enum() => {
123+
(ty::Adt(def, _), Some(to_nbits)) if def.is_enum() => {
125124
let (from_nbits, variant) = if let ExprKind::Path(p) = &cast_expr.kind
126125
&& let Res::Def(DefKind::Ctor(..), id) = cx.qpath_res(p, cast_expr.hir_id)
127126
{
@@ -132,7 +131,6 @@ pub(super) fn check(
132131
} else {
133132
(utils::enum_ty_to_nbits(*def, cx.tcx), None)
134133
};
135-
let to_nbits = utils::int_ty_to_nbits(cast_to, cx.tcx);
136134

137135
let cast_from_ptr_size = def.repr().int.is_none_or(|ty| matches!(ty, IntegerType::Pointer(_),));
138136
let suffix = match (cast_from_ptr_size, is_isize_or_usize(cast_to)) {
@@ -157,11 +155,11 @@ pub(super) fn check(
157155
format!("casting `{cast_from}` to `{cast_to}` may truncate the value{suffix}")
158156
},
159157

160-
(ty::Float(_), true) => {
158+
(ty::Float(_), Some(_)) => {
161159
format!("casting `{cast_from}` to `{cast_to}` may truncate the value")
162160
},
163161

164-
(ty::Float(FloatTy::F64), false) if matches!(cast_to.kind(), &ty::Float(FloatTy::F32)) => {
162+
(ty::Float(FloatTy::F64), None) if matches!(cast_to.kind(), &ty::Float(FloatTy::F32)) => {
165163
"casting `f64` to `f32` may truncate the value".to_string()
166164
},
167165

clippy_lints/src/casts/cast_possible_wrap.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ enum EmitState {
1717
}
1818

1919
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>) {
20-
if !(cast_from.is_integral() && cast_to.is_integral()) {
20+
let (Some(from_nbits), Some(to_nbits)) = (
21+
utils::int_ty_to_nbits(cx.tcx, cast_from),
22+
utils::int_ty_to_nbits(cx.tcx, cast_to),
23+
) else {
2124
return;
22-
}
25+
};
2326

2427
// emit a lint if a cast is:
2528
// 1. unsigned to signed
@@ -35,9 +38,6 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_from: Ty<'_>, ca
3538
return;
3639
}
3740

38-
let from_nbits = utils::int_ty_to_nbits(cast_from, cx.tcx);
39-
let to_nbits = utils::int_ty_to_nbits(cast_to, cx.tcx);
40-
4141
let should_lint = match (cast_from.is_ptr_sized_integral(), cast_to.is_ptr_sized_integral()) {
4242
(true, true) => {
4343
// casts between two ptr sized integers are trivially always the same size

clippy_lints/src/casts/cast_precision_loss.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ use rustc_middle::ty::{self, FloatTy, Ty};
77
use super::{CAST_PRECISION_LOSS, utils};
88

99
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>) {
10-
if !cast_from.is_integral() || cast_to.is_integral() {
10+
let Some(from_nbits) = utils::int_ty_to_nbits(cx.tcx, cast_from) else {
1111
return;
12-
}
12+
};
1313

14-
let from_nbits = utils::int_ty_to_nbits(cast_from, cx.tcx);
15-
let to_nbits = if cast_to.kind() == &ty::Float(FloatTy::F32) {
16-
32
17-
} else {
18-
64
14+
// FIXME: handle `f16` and `f128`
15+
let to_nbits = match cast_to.kind() {
16+
ty::Float(f @ (FloatTy::F32 | FloatTy::F64)) => f.bit_width(),
17+
_ => return,
1918
};
2019

2120
if !(is_isize_or_usize(cast_from) || from_nbits >= to_nbits) {
@@ -29,9 +28,10 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_from: Ty<'_>, ca
2928
let from_nbits_str = if arch_dependent {
3029
"64".to_owned()
3130
} else if is_isize_or_usize(cast_from) {
31+
// FIXME: handle 16 bits `usize` type
3232
"32 or 64".to_owned()
3333
} else {
34-
utils::int_ty_to_nbits(cast_from, cx.tcx).to_string()
34+
from_nbits.to_string()
3535
};
3636

3737
span_lint(

clippy_lints/src/casts/cast_ptr_alignment.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ fn is_used_as_unaligned(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
6161
};
6262
match parent.kind {
6363
ExprKind::MethodCall(name, self_arg, ..) if self_arg.hir_id == e.hir_id => {
64-
if matches!(name.ident.as_str(), "read_unaligned" | "write_unaligned")
64+
if matches!(name.ident.name, sym::read_unaligned | sym::write_unaligned)
6565
&& let Some(def_id) = cx.typeck_results().type_dependent_def_id(parent.hir_id)
6666
&& let Some(def_id) = cx.tcx.impl_of_method(def_id)
6767
&& cx.tcx.type_of(def_id).instantiate_identity().is_raw_ptr()

0 commit comments

Comments
 (0)