Skip to content

Commit 29bd9b3

Browse files
committed
Merge commit '88f787d193fb1f0491b001288e82b5574c080606' into clippy-subtree-update
2 parents 2972b5e + 88f787d commit 29bd9b3

55 files changed

Lines changed: 1979 additions & 669 deletions

Some content is hidden

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

src/tools/clippy/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6797,9 +6797,11 @@ Released 2018-09-13
67976797
[`manual_midpoint`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_midpoint
67986798
[`manual_next_back`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_next_back
67996799
[`manual_non_exhaustive`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_non_exhaustive
6800+
[`manual_noop_waker`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_noop_waker
68006801
[`manual_ok_err`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_ok_err
68016802
[`manual_ok_or`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_ok_or
68026803
[`manual_option_as_slice`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_option_as_slice
6804+
[`manual_option_zip`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_option_zip
68036805
[`manual_pattern_char_comparison`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_pattern_char_comparison
68046806
[`manual_pop_if`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_pop_if
68056807
[`manual_range_contains`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_range_contains

src/tools/clippy/clippy_config/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
unused_qualifications
88
)]
99
#![allow(clippy::must_use_candidate, clippy::missing_panics_doc)]
10-
#![deny(clippy::derive_deserialize_allowing_unknown)]
1110

1211
extern crate rustc_data_structures;
1312
extern crate rustc_errors;

src/tools/clippy/clippy_dev/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#![feature(
22
exit_status_error,
33
new_range,
4-
new_range_api,
54
os_str_slice,
65
os_string_truncate,
76
pattern,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,8 @@ fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String {
275275
let _: fmt::Result = writedoc!(
276276
result,
277277
r"
278-
use clippy_utils::msrvs::{{self, {msrv_ty}}};
279278
use clippy_config::Conf;
279+
use clippy_utils::msrvs::{{self, {msrv_ty}}};
280280
{pass_import}
281281
use rustc_lint::{{{context_import}, {pass_type}}};
282282
use rustc_session::impl_lint_pass;
@@ -319,7 +319,7 @@ fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String {
319319
320320
impl {pass_type}{pass_lifetimes} for {name_camel} {{{extract_msrv}}}
321321
322-
// TODO: Add MSRV level to `clippy_config/src/msrvs.rs` if needed.
322+
// TODO: Add MSRV level to `clippy_utils/src/msrvs.rs` if needed.
323323
// TODO: Update msrv config comment in `clippy_config/src/conf.rs`
324324
"
325325
);

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

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::numeric_literal::NumericLiteral;
33
use clippy_utils::res::MaybeResPath as _;
4-
use clippy_utils::source::{SpanRangeExt, snippet_opt};
4+
use clippy_utils::source::{SpanRangeExt, snippet, snippet_with_applicability};
5+
use clippy_utils::sugg::has_enclosing_paren;
56
use clippy_utils::visitors::{Visitable, for_each_expr_without_closures};
67
use clippy_utils::{get_parent_expr, is_hir_ty_cfg_dependant, is_ty_alias, sym};
78
use rustc_ast::{LitFloatType, LitIntType, LitKind};
@@ -24,7 +25,8 @@ pub(super) fn check<'tcx>(
2425
cast_from: Ty<'tcx>,
2526
cast_to: Ty<'tcx>,
2627
) -> bool {
27-
let cast_str = snippet_opt(cx, cast_expr.span).unwrap_or_default();
28+
let mut app = Applicability::MachineApplicable;
29+
let cast_str = snippet_with_applicability(cx, cast_expr.span, "_", &mut app);
2830

2931
if let ty::RawPtr(..) = cast_from.kind()
3032
// check both mutability and type are the same
@@ -47,16 +49,23 @@ pub(super) fn check<'tcx>(
4749
_ => {},
4850
}
4951

50-
span_lint_and_sugg(
52+
// Preserve parentheses around `expr` in case of cascaded casts
53+
let surrounding =
54+
if matches!(cast_expr.kind, ExprKind::Cast(..)) && has_enclosing_paren(snippet(cx, expr.span, "")) {
55+
MaybeParenOrBlock::Paren
56+
} else {
57+
MaybeParenOrBlock::Nothing
58+
};
59+
60+
emit_lint(
5161
cx,
52-
UNNECESSARY_CAST,
53-
expr.span,
62+
expr,
5463
format!(
5564
"casting raw pointers to the same type and constness is unnecessary (`{cast_from}` -> `{cast_to}`)"
5665
),
57-
"try",
58-
cast_str.clone(),
59-
Applicability::MaybeIncorrect,
66+
&cast_str,
67+
surrounding,
68+
app.max(Applicability::MaybeIncorrect),
6069
);
6170
}
6271

@@ -143,12 +152,6 @@ pub(super) fn check<'tcx>(
143152
}
144153

145154
if cast_from.kind() == cast_to.kind() && !expr.span.in_external_macro(cx.sess().source_map()) {
146-
enum MaybeParenOrBlock {
147-
Paren,
148-
Block,
149-
Nothing,
150-
}
151-
152155
fn is_borrow_expr(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
153156
matches!(expr.kind, ExprKind::AddrOf(..))
154157
|| cx
@@ -188,18 +191,13 @@ pub(super) fn check<'tcx>(
188191
_ => MaybeParenOrBlock::Nothing,
189192
};
190193

191-
span_lint_and_sugg(
194+
emit_lint(
192195
cx,
193-
UNNECESSARY_CAST,
194-
expr.span,
196+
expr,
195197
format!("casting to the same type is unnecessary (`{cast_from}` -> `{cast_to}`)"),
196-
"try",
197-
match surrounding {
198-
MaybeParenOrBlock::Paren => format!("({cast_str})"),
199-
MaybeParenOrBlock::Block => format!("{{ {cast_str} }}"),
200-
MaybeParenOrBlock::Nothing => cast_str,
201-
},
202-
Applicability::MachineApplicable,
198+
&cast_str,
199+
surrounding,
200+
app,
203201
);
204202
return true;
205203
}
@@ -312,3 +310,33 @@ fn is_cast_from_ty_alias<'tcx>(cx: &LateContext<'tcx>, expr: impl Visitable<'tcx
312310
})
313311
.is_some()
314312
}
313+
314+
#[derive(Clone, Copy)]
315+
enum MaybeParenOrBlock {
316+
Paren,
317+
Block,
318+
Nothing,
319+
}
320+
321+
fn emit_lint(
322+
cx: &LateContext<'_>,
323+
expr: &Expr<'_>,
324+
msg: String,
325+
sugg: &str,
326+
surrounding: MaybeParenOrBlock,
327+
applicability: Applicability,
328+
) {
329+
span_lint_and_sugg(
330+
cx,
331+
UNNECESSARY_CAST,
332+
expr.span,
333+
msg,
334+
"try",
335+
match surrounding {
336+
MaybeParenOrBlock::Paren => format!("({sugg})"),
337+
MaybeParenOrBlock::Block => format!("{{ {sugg} }}"),
338+
MaybeParenOrBlock::Nothing => sugg.to_string(),
339+
},
340+
applicability,
341+
);
342+
}

src/tools/clippy/clippy_lints/src/collapsible_if.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clippy_config::Conf;
22
use clippy_utils::diagnostics::span_lint_hir_and_then;
33
use clippy_utils::msrvs::Msrv;
44
use clippy_utils::source::{HasSession, IntoSpan as _, SpanRangeExt, snippet, snippet_block_with_applicability};
5-
use clippy_utils::{can_use_if_let_chains, span_contains_non_whitespace, sym, tokenize_with_text};
5+
use clippy_utils::{can_use_if_let_chains, span_contains_cfg, span_contains_non_whitespace, sym, tokenize_with_text};
66
use rustc_ast::BinOpKind;
77
use rustc_errors::Applicability;
88
use rustc_hir::attrs::{AttributeKind, LintAttributeKind};
@@ -170,6 +170,11 @@ impl CollapsibleIf {
170170
&& self.eligible_condition(cx, check_inner)
171171
&& expr.span.eq_ctxt(inner.span)
172172
&& self.check_significant_tokens_and_expect_attrs(cx, then, inner, sym::collapsible_if)
173+
&& let then_closing_bracket = {
174+
let end = then.span.shrink_to_hi();
175+
end.with_lo(end.lo() - BytePos(1))
176+
}
177+
&& !span_contains_cfg(cx, inner.span.between(then_closing_bracket))
173178
{
174179
span_lint_hir_and_then(
175180
cx,
@@ -179,12 +184,7 @@ impl CollapsibleIf {
179184
"this `if` statement can be collapsed",
180185
|diag| {
181186
let then_open_bracket = then.span.split_at(1).0.with_leading_whitespace(cx).into_span();
182-
let then_closing_bracket = {
183-
let end = then.span.shrink_to_hi();
184-
end.with_lo(end.lo() - BytePos(1))
185-
.with_leading_whitespace(cx)
186-
.into_span()
187-
};
187+
let then_closing_bracket = then_closing_bracket.with_leading_whitespace(cx).into_span();
188188
let (paren_start, inner_if_span, paren_end) = peel_parens(cx, inner.span);
189189
let inner_if = inner_if_span.split_at(2).0;
190190
let mut sugg = vec![
@@ -238,12 +238,10 @@ impl CollapsibleIf {
238238
!span_contains_non_whitespace(cx, span, self.lint_commented_code)
239239
},
240240

241-
[
242-
Attribute::Parsed(AttributeKind::LintAttributes(sub_attrs)),
243-
] => {
241+
[Attribute::Parsed(AttributeKind::LintAttributes(sub_attrs))] => {
244242
sub_attrs
245243
.into_iter()
246-
.filter(|attr|attr.kind == LintAttributeKind::Expect)
244+
.filter(|attr| attr.kind == LintAttributeKind::Expect)
247245
.flat_map(|attr| attr.lint_instances.iter().map(|group| (attr.attr_span, group)))
248246
.filter(|(_, lint_id)| {
249247
lint_id.tool_is_named(sym::clippy)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ pub static LINTS: &[&::declare_clippy_lint::LintInfo] = &[
311311
crate::manual_let_else::MANUAL_LET_ELSE_INFO,
312312
crate::manual_main_separator_str::MANUAL_MAIN_SEPARATOR_STR_INFO,
313313
crate::manual_non_exhaustive::MANUAL_NON_EXHAUSTIVE_INFO,
314+
crate::manual_noop_waker::MANUAL_NOOP_WAKER_INFO,
314315
crate::manual_option_as_slice::MANUAL_OPTION_AS_SLICE_INFO,
315316
crate::manual_pop_if::MANUAL_POP_IF_INFO,
316317
crate::manual_range_patterns::MANUAL_RANGE_PATTERNS_INFO,
@@ -418,6 +419,7 @@ pub static LINTS: &[&::declare_clippy_lint::LintInfo] = &[
418419
crate::methods::MANUAL_IS_VARIANT_AND_INFO,
419420
crate::methods::MANUAL_NEXT_BACK_INFO,
420421
crate::methods::MANUAL_OK_OR_INFO,
422+
crate::methods::MANUAL_OPTION_ZIP_INFO,
421423
crate::methods::MANUAL_REPEAT_N_INFO,
422424
crate::methods::MANUAL_SATURATING_ARITHMETIC_INFO,
423425
crate::methods::MANUAL_SPLIT_ONCE_INFO,

src/tools/clippy/clippy_lints/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ mod manual_is_power_of_two;
209209
mod manual_let_else;
210210
mod manual_main_separator_str;
211211
mod manual_non_exhaustive;
212+
mod manual_noop_waker;
212213
mod manual_option_as_slice;
213214
mod manual_pop_if;
214215
mod manual_range_patterns;
@@ -864,7 +865,8 @@ pub fn register_lint_passes(store: &mut rustc_lint::LintStore, conf: &'static Co
864865
Box::new(move |tcx| Box::new(duration_suboptimal_units::DurationSuboptimalUnits::new(tcx, conf))),
865866
Box::new(move |_| Box::new(manual_take::ManualTake::new(conf))),
866867
Box::new(|_| Box::new(manual_checked_ops::ManualCheckedOps)),
867-
Box::new(move |_| Box::new(manual_pop_if::ManualPopIf::new(conf))),
868+
Box::new(move |tcx| Box::new(manual_pop_if::ManualPopIf::new(tcx, conf))),
869+
Box::new(|_| Box::new(manual_noop_waker::ManualNoopWaker)),
868870
// add late passes here, used by `cargo dev new_lint`
869871
];
870872
store.late_passes.extend(late_lints);

src/tools/clippy/clippy_lints/src/loops/explicit_counter_loop.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::borrow::Cow;
22

33
use super::{EXPLICIT_COUNTER_LOOP, IncrementVisitor, InitializeVisitor, make_iterator_snippet};
44
use clippy_utils::diagnostics::span_lint_and_then;
5+
use clippy_utils::higher::Range;
56
use clippy_utils::source::snippet_with_applicability;
67
use clippy_utils::sugg::{EMPTY, Sugg};
78
use clippy_utils::{get_enclosing_block, is_integer_const, is_integer_literal_untyped};
@@ -83,6 +84,26 @@ pub(super) fn check<'tcx>(
8384
snippet
8485
});
8586

87+
// If the loop variable is unused and the range is `0..n`, suggest `(init..).take(n)`.
88+
if pat_snippet == "_"
89+
&& let Some(range) = Range::hir(cx, arg)
90+
&& range.limits == RangeLimits::HalfOpen
91+
&& range.start.is_some_and(|start| is_integer_const(cx, start, 0))
92+
&& let Some(end) = range.end
93+
{
94+
let end = snippet_with_applicability(cx, end.span, "..", &mut applicability);
95+
diag.span_suggestion(
96+
span,
97+
"consider using",
98+
format!(
99+
"{loop_label}for {name} in ({}).take({end})",
100+
initializer.range(&EMPTY, RangeLimits::HalfOpen)
101+
),
102+
applicability,
103+
);
104+
return;
105+
}
106+
86107
diag.span_suggestion(
87108
span,
88109
"consider using",

src/tools/clippy/clippy_lints/src/loops/manual_memcpy.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,8 @@ fn get_details_from_idx<'tcx>(
385385
ExprKind::Binary(op, lhs, rhs) => match op.node {
386386
BinOpKind::Add => {
387387
let offset_opt = get_start(lhs, starts)
388-
.and_then(|s| get_offset(cx, rhs, starts).map(|o| (s, o)))
389-
.or_else(|| get_start(rhs, starts).and_then(|s| get_offset(cx, lhs, starts).map(|o| (s, o))));
388+
.zip(get_offset(cx, rhs, starts))
389+
.or_else(|| get_start(rhs, starts).zip(get_offset(cx, lhs, starts)));
390390

391391
offset_opt.map(|(s, o)| (s, Offset::positive(o)))
392392
},

0 commit comments

Comments
 (0)