Skip to content

Commit 56e35a5

Browse files
committed
Auto merge of #131205 - flip1995:clippy-subtree-update, r=Manishearth
Clippy subtree update r? `@Manishearth`
2 parents 1e5719b + b823547 commit 56e35a5

File tree

183 files changed

+2484
-848
lines changed

Some content is hidden

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

183 files changed

+2484
-848
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
uses: actions/checkout@v4
2020

2121
- name: Setup Node.js
22-
uses: actions/setup-node@v3
22+
uses: actions/setup-node@v4
2323
with:
2424
node-version: '18.x'
2525

src/tools/clippy/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5871,6 +5871,7 @@ Released 2018-09-13
58715871
[`ref_as_ptr`]: https://rust-lang.github.io/rust-clippy/master/index.html#ref_as_ptr
58725872
[`ref_binding_to_reference`]: https://rust-lang.github.io/rust-clippy/master/index.html#ref_binding_to_reference
58735873
[`ref_in_deref`]: https://rust-lang.github.io/rust-clippy/master/index.html#ref_in_deref
5874+
[`ref_option`]: https://rust-lang.github.io/rust-clippy/master/index.html#ref_option
58745875
[`ref_option_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#ref_option_ref
58755876
[`ref_patterns`]: https://rust-lang.github.io/rust-clippy/master/index.html#ref_patterns
58765877
[`regex_macro`]: https://rust-lang.github.io/rust-clippy/master/index.html#regex_macro

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

+1
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ Suppress lints whenever the suggested change would cause breakage for other crat
353353
* [`rc_buffer`](https://rust-lang.github.io/rust-clippy/master/index.html#rc_buffer)
354354
* [`rc_mutex`](https://rust-lang.github.io/rust-clippy/master/index.html#rc_mutex)
355355
* [`redundant_allocation`](https://rust-lang.github.io/rust-clippy/master/index.html#redundant_allocation)
356+
* [`ref_option`](https://rust-lang.github.io/rust-clippy/master/index.html#ref_option)
356357
* [`single_call_fn`](https://rust-lang.github.io/rust-clippy/master/index.html#single_call_fn)
357358
* [`trivially_copy_pass_by_ref`](https://rust-lang.github.io/rust-clippy/master/index.html#trivially_copy_pass_by_ref)
358359
* [`unnecessary_box_returns`](https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_box_returns)

src/tools/clippy/clippy_config/src/conf.rs

+1
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ define_Conf! {
378378
rc_buffer,
379379
rc_mutex,
380380
redundant_allocation,
381+
ref_option,
381382
single_call_fn,
382383
trivially_copy_pass_by_ref,
383384
unnecessary_box_returns,

src/tools/clippy/clippy_config/src/msrvs.rs

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ macro_rules! msrv_aliases {
1919
msrv_aliases! {
2020
1,83,0 { CONST_EXTERN_FN }
2121
1,83,0 { CONST_FLOAT_BITS_CONV }
22+
1,82,0 { IS_NONE_OR }
2223
1,81,0 { LINT_REASONS_STABILIZATION }
2324
1,80,0 { BOX_INTO_ITER}
2425
1,77,0 { C_STR_LITERALS }

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

+64-22
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use clippy_config::Conf;
2+
use clippy_config::msrvs::{self, Msrv};
13
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_hir_and_then};
24
use clippy_utils::eq_expr_value;
35
use clippy_utils::source::SpanRangeExt;
@@ -7,7 +9,7 @@ use rustc_errors::Applicability;
79
use rustc_hir::intravisit::{FnKind, Visitor, walk_expr};
810
use rustc_hir::{BinOpKind, Body, Expr, ExprKind, FnDecl, UnOp};
911
use rustc_lint::{LateContext, LateLintPass, Level};
10-
use rustc_session::declare_lint_pass;
12+
use rustc_session::{RustcVersion, impl_lint_pass};
1113
use rustc_span::def_id::LocalDefId;
1214
use rustc_span::{Span, sym};
1315

@@ -69,9 +71,25 @@ declare_clippy_lint! {
6971
}
7072

7173
// For each pairs, both orders are considered.
72-
const METHODS_WITH_NEGATION: [(&str, &str); 2] = [("is_some", "is_none"), ("is_err", "is_ok")];
74+
const METHODS_WITH_NEGATION: [(Option<RustcVersion>, &str, &str); 3] = [
75+
(None, "is_some", "is_none"),
76+
(None, "is_err", "is_ok"),
77+
(Some(msrvs::IS_NONE_OR), "is_some_and", "is_none_or"),
78+
];
79+
80+
pub struct NonminimalBool {
81+
msrv: Msrv,
82+
}
83+
84+
impl NonminimalBool {
85+
pub fn new(conf: &'static Conf) -> Self {
86+
Self {
87+
msrv: conf.msrv.clone(),
88+
}
89+
}
90+
}
7391

74-
declare_lint_pass!(NonminimalBool => [NONMINIMAL_BOOL, OVERLY_COMPLEX_BOOL_EXPR]);
92+
impl_lint_pass!(NonminimalBool => [NONMINIMAL_BOOL, OVERLY_COMPLEX_BOOL_EXPR]);
7593

7694
impl<'tcx> LateLintPass<'tcx> for NonminimalBool {
7795
fn check_fn(
@@ -83,7 +101,7 @@ impl<'tcx> LateLintPass<'tcx> for NonminimalBool {
83101
_: Span,
84102
_: LocalDefId,
85103
) {
86-
NonminimalBoolVisitor { cx }.visit_body(body);
104+
NonminimalBoolVisitor { cx, msrv: &self.msrv }.visit_body(body);
87105
}
88106

89107
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
@@ -100,6 +118,8 @@ impl<'tcx> LateLintPass<'tcx> for NonminimalBool {
100118
_ => {},
101119
}
102120
}
121+
122+
extract_msrv_attr!(LateContext);
103123
}
104124

105125
fn inverted_bin_op_eq_str(op: BinOpKind) -> Option<&'static str> {
@@ -176,11 +196,11 @@ fn check_inverted_bool_in_condition(
176196
);
177197
}
178198

179-
fn check_simplify_not(cx: &LateContext<'_>, expr: &Expr<'_>) {
199+
fn check_simplify_not(cx: &LateContext<'_>, msrv: &Msrv, expr: &Expr<'_>) {
180200
if let ExprKind::Unary(UnOp::Not, inner) = &expr.kind
181201
&& !expr.span.from_expansion()
182202
&& !inner.span.from_expansion()
183-
&& let Some(suggestion) = simplify_not(cx, inner)
203+
&& let Some(suggestion) = simplify_not(cx, msrv, inner)
184204
&& cx.tcx.lint_level_at_node(NONMINIMAL_BOOL, expr.hir_id).0 != Level::Allow
185205
{
186206
span_lint_and_sugg(
@@ -197,6 +217,7 @@ fn check_simplify_not(cx: &LateContext<'_>, expr: &Expr<'_>) {
197217

198218
struct NonminimalBoolVisitor<'a, 'tcx> {
199219
cx: &'a LateContext<'tcx>,
220+
msrv: &'a Msrv,
200221
}
201222

202223
use quine_mc_cluskey::Bool;
@@ -205,7 +226,7 @@ struct Hir2Qmm<'a, 'tcx, 'v> {
205226
cx: &'a LateContext<'tcx>,
206227
}
207228

208-
impl<'a, 'tcx, 'v> Hir2Qmm<'a, 'tcx, 'v> {
229+
impl<'v> Hir2Qmm<'_, '_, 'v> {
209230
fn extract(&mut self, op: BinOpKind, a: &[&'v Expr<'_>], mut v: Vec<Bool>) -> Result<Vec<Bool>, String> {
210231
for a in a {
211232
if let ExprKind::Binary(binop, lhs, rhs) = &a.kind {
@@ -289,10 +310,11 @@ impl<'a, 'tcx, 'v> Hir2Qmm<'a, 'tcx, 'v> {
289310
struct SuggestContext<'a, 'tcx, 'v> {
290311
terminals: &'v [&'v Expr<'v>],
291312
cx: &'a LateContext<'tcx>,
313+
msrv: &'a Msrv,
292314
output: String,
293315
}
294316

295-
impl<'a, 'tcx, 'v> SuggestContext<'a, 'tcx, 'v> {
317+
impl SuggestContext<'_, '_, '_> {
296318
fn recurse(&mut self, suggestion: &Bool) -> Option<()> {
297319
use quine_mc_cluskey::Bool::{And, False, Not, Or, Term, True};
298320
match suggestion {
@@ -311,7 +333,7 @@ impl<'a, 'tcx, 'v> SuggestContext<'a, 'tcx, 'v> {
311333
},
312334
Term(n) => {
313335
let terminal = self.terminals[n as usize];
314-
if let Some(str) = simplify_not(self.cx, terminal) {
336+
if let Some(str) = simplify_not(self.cx, self.msrv, terminal) {
315337
self.output.push_str(&str);
316338
} else {
317339
self.output.push('!');
@@ -358,7 +380,7 @@ impl<'a, 'tcx, 'v> SuggestContext<'a, 'tcx, 'v> {
358380
}
359381
}
360382

361-
fn simplify_not(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<String> {
383+
fn simplify_not(cx: &LateContext<'_>, curr_msrv: &Msrv, expr: &Expr<'_>) -> Option<String> {
362384
match &expr.kind {
363385
ExprKind::Binary(binop, lhs, rhs) => {
364386
if !implements_ord(cx, lhs) {
@@ -389,7 +411,7 @@ fn simplify_not(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<String> {
389411
Some(format!("{lhs_snippet}{op}{rhs_snippet}"))
390412
})
391413
},
392-
ExprKind::MethodCall(path, receiver, [], _) => {
414+
ExprKind::MethodCall(path, receiver, args, _) => {
393415
let type_of_receiver = cx.typeck_results().expr_ty(receiver);
394416
if !is_type_diagnostic_item(cx, type_of_receiver, sym::Option)
395417
&& !is_type_diagnostic_item(cx, type_of_receiver, sym::Result)
@@ -399,21 +421,41 @@ fn simplify_not(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<String> {
399421
METHODS_WITH_NEGATION
400422
.iter()
401423
.copied()
402-
.flat_map(|(a, b)| vec![(a, b), (b, a)])
403-
.find(|&(a, _)| {
404-
let path: &str = path.ident.name.as_str();
405-
a == path
424+
.flat_map(|(msrv, a, b)| vec![(msrv, a, b), (msrv, b, a)])
425+
.find(|&(msrv, a, _)| msrv.is_none_or(|msrv| curr_msrv.meets(msrv)) && a == path.ident.name.as_str())
426+
.and_then(|(_, _, neg_method)| {
427+
let negated_args = args
428+
.iter()
429+
.map(|arg| simplify_not(cx, curr_msrv, arg))
430+
.collect::<Option<Vec<_>>>()?
431+
.join(", ");
432+
Some(format!(
433+
"{}.{neg_method}({negated_args})",
434+
receiver.span.get_source_text(cx)?
435+
))
406436
})
407-
.and_then(|(_, neg_method)| Some(format!("{}.{neg_method}()", receiver.span.get_source_text(cx)?)))
408437
},
438+
ExprKind::Closure(closure) => {
439+
let body = cx.tcx.hir().body(closure.body);
440+
let params = body
441+
.params
442+
.iter()
443+
.map(|param| param.span.get_source_text(cx).map(|t| t.to_string()))
444+
.collect::<Option<Vec<_>>>()?
445+
.join(", ");
446+
let negated = simplify_not(cx, curr_msrv, body.value)?;
447+
Some(format!("|{params}| {negated}"))
448+
},
449+
ExprKind::Unary(UnOp::Not, expr) => expr.span.get_source_text(cx).map(|t| t.to_string()),
409450
_ => None,
410451
}
411452
}
412453

413-
fn suggest(cx: &LateContext<'_>, suggestion: &Bool, terminals: &[&Expr<'_>]) -> String {
454+
fn suggest(cx: &LateContext<'_>, msrv: &Msrv, suggestion: &Bool, terminals: &[&Expr<'_>]) -> String {
414455
let mut suggest_context = SuggestContext {
415456
terminals,
416457
cx,
458+
msrv,
417459
output: String::new(),
418460
};
419461
suggest_context.recurse(suggestion);
@@ -475,7 +517,7 @@ fn terminal_stats(b: &Bool) -> Stats {
475517
stats
476518
}
477519

478-
impl<'a, 'tcx> NonminimalBoolVisitor<'a, 'tcx> {
520+
impl<'tcx> NonminimalBoolVisitor<'_, 'tcx> {
479521
fn bool_expr(&self, e: &'tcx Expr<'_>) {
480522
let mut h2q = Hir2Qmm {
481523
terminals: Vec::new(),
@@ -526,7 +568,7 @@ impl<'a, 'tcx> NonminimalBoolVisitor<'a, 'tcx> {
526568
diag.span_suggestion(
527569
e.span,
528570
"it would look like the following",
529-
suggest(self.cx, suggestion, &h2q.terminals),
571+
suggest(self.cx, self.msrv, suggestion, &h2q.terminals),
530572
// nonminimal_bool can produce minimal but
531573
// not human readable expressions (#3141)
532574
Applicability::Unspecified,
@@ -569,20 +611,20 @@ impl<'a, 'tcx> NonminimalBoolVisitor<'a, 'tcx> {
569611
}
570612
};
571613
if improvements.is_empty() {
572-
check_simplify_not(self.cx, e);
614+
check_simplify_not(self.cx, self.msrv, e);
573615
} else {
574616
nonminimal_bool_lint(
575617
improvements
576618
.into_iter()
577-
.map(|suggestion| suggest(self.cx, suggestion, &h2q.terminals))
619+
.map(|suggestion| suggest(self.cx, self.msrv, suggestion, &h2q.terminals))
578620
.collect(),
579621
);
580622
}
581623
}
582624
}
583625
}
584626

585-
impl<'a, 'tcx> Visitor<'tcx> for NonminimalBoolVisitor<'a, 'tcx> {
627+
impl<'tcx> Visitor<'tcx> for NonminimalBoolVisitor<'_, 'tcx> {
586628
fn visit_expr(&mut self, e: &'tcx Expr<'_>) {
587629
if !e.span.from_expansion() {
588630
match &e.kind {

src/tools/clippy/clippy_lints/src/box_default.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ fn is_local_vec_expn(cx: &LateContext<'_>, expr: &Expr<'_>, ref_expr: &Expr<'_>)
9191
#[derive(Default)]
9292
struct InferVisitor(bool);
9393

94-
impl<'tcx> Visitor<'tcx> for InferVisitor {
94+
impl Visitor<'_> for InferVisitor {
9595
fn visit_ty(&mut self, t: &Ty<'_>) {
9696
self.0 |= matches!(t.kind, TyKind::Infer | TyKind::OpaqueDef(..) | TyKind::TraitObject(..));
9797
if !self.0 {

src/tools/clippy/clippy_lints/src/cargo/common_metadata.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,27 @@ pub(super) fn check(cx: &LateContext<'_>, metadata: &Metadata, ignore_publish: b
1010
// only run the lint if publish is `None` (`publish = true` or skipped entirely)
1111
// or if the vector isn't empty (`publish = ["something"]`)
1212
if package.publish.as_ref().filter(|publish| publish.is_empty()).is_none() || ignore_publish {
13-
if is_empty_str(&package.description) {
13+
if is_empty_str(package.description.as_ref()) {
1414
missing_warning(cx, package, "package.description");
1515
}
1616

17-
if is_empty_str(&package.license) && is_empty_str(&package.license_file) {
17+
if is_empty_str(package.license.as_ref()) && is_empty_str(package.license_file.as_ref()) {
1818
missing_warning(cx, package, "either package.license or package.license_file");
1919
}
2020

21-
if is_empty_str(&package.repository) {
21+
if is_empty_str(package.repository.as_ref()) {
2222
missing_warning(cx, package, "package.repository");
2323
}
2424

25-
if is_empty_str(&package.readme) {
25+
if is_empty_str(package.readme.as_ref()) {
2626
missing_warning(cx, package, "package.readme");
2727
}
2828

29-
if is_empty_vec(&package.keywords) {
29+
if is_empty_vec(package.keywords.as_ref()) {
3030
missing_warning(cx, package, "package.keywords");
3131
}
3232

33-
if is_empty_vec(&package.categories) {
33+
if is_empty_vec(package.categories.as_ref()) {
3434
missing_warning(cx, package, "package.categories");
3535
}
3636
}
@@ -42,8 +42,8 @@ fn missing_warning(cx: &LateContext<'_>, package: &cargo_metadata::Package, fiel
4242
span_lint(cx, CARGO_COMMON_METADATA, DUMMY_SP, message);
4343
}
4444

45-
fn is_empty_str<T: AsRef<std::ffi::OsStr>>(value: &Option<T>) -> bool {
46-
value.as_ref().map_or(true, |s| s.as_ref().is_empty())
45+
fn is_empty_str<T: AsRef<std::ffi::OsStr>>(value: Option<&T>) -> bool {
46+
value.map_or(true, |s| s.as_ref().is_empty())
4747
}
4848

4949
fn is_empty_vec(value: &[String]) -> bool {

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::is_no_std_crate;
32
use clippy_utils::source::snippet_with_context;
3+
use clippy_utils::std_or_core;
44
use rustc_errors::Applicability;
55
use rustc_hir::{BorrowKind, Expr, ExprKind, Mutability, Ty, TyKind};
66
use rustc_lint::LateContext;
@@ -16,8 +16,8 @@ pub(super) fn check<'tcx>(
1616
) {
1717
if matches!(cast_to.kind, TyKind::Ptr(_))
1818
&& let ExprKind::AddrOf(BorrowKind::Ref, mutability, e) = cast_expr.kind
19+
&& let Some(std_or_core) = std_or_core(cx)
1920
{
20-
let core_or_std = if is_no_std_crate(cx) { "core" } else { "std" };
2121
let macro_name = match mutability {
2222
Mutability::Not => "addr_of",
2323
Mutability::Mut => "addr_of_mut",
@@ -40,7 +40,7 @@ pub(super) fn check<'tcx>(
4040
expr.span,
4141
"borrow as raw pointer",
4242
"try",
43-
format!("{core_or_std}::ptr::{macro_name}!({snip})"),
43+
format!("{std_or_core}::ptr::{macro_name}!({snip})"),
4444
Applicability::MachineApplicable,
4545
);
4646
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::snippet_with_applicability;
33
use clippy_utils::sugg::Sugg;
4-
use clippy_utils::{ExprUseNode, expr_use_ctxt, is_no_std_crate};
4+
use clippy_utils::{ExprUseNode, expr_use_ctxt, std_or_core};
55
use rustc_errors::Applicability;
66
use rustc_hir::{Expr, Mutability, Ty, TyKind};
77
use rustc_lint::LateContext;
@@ -25,8 +25,8 @@ pub(super) fn check<'tcx>(
2525
&& let use_cx = expr_use_ctxt(cx, expr)
2626
// TODO: only block the lint if `cast_expr` is a temporary
2727
&& !matches!(use_cx.use_node(cx), ExprUseNode::LetStmt(_) | ExprUseNode::ConstStatic(_))
28+
&& let Some(std_or_core) = std_or_core(cx)
2829
{
29-
let core_or_std = if is_no_std_crate(cx) { "core" } else { "std" };
3030
let fn_name = match to_mutbl {
3131
Mutability::Not => "from_ref",
3232
Mutability::Mut => "from_mut",
@@ -56,7 +56,7 @@ pub(super) fn check<'tcx>(
5656
expr.span,
5757
"reference as raw pointer",
5858
"try",
59-
format!("{core_or_std}::ptr::{fn_name}{turbofish}({cast_expr_sugg})"),
59+
format!("{std_or_core}::ptr::{fn_name}{turbofish}({cast_expr_sugg})"),
6060
app,
6161
);
6262
}

src/tools/clippy/clippy_lints/src/checked_conversions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl CheckedConversions {
4848

4949
impl_lint_pass!(CheckedConversions => [CHECKED_CONVERSIONS]);
5050

51-
impl<'tcx> LateLintPass<'tcx> for CheckedConversions {
51+
impl LateLintPass<'_> for CheckedConversions {
5252
fn check_expr(&mut self, cx: &LateContext<'_>, item: &Expr<'_>) {
5353
if let ExprKind::Binary(op, lhs, rhs) = item.kind
5454
&& let (lt1, gt1, op2) = match op.node {

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

+1
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ pub static LINTS: &[&crate::LintInfo] = &[
202202
crate::functions::MUST_USE_CANDIDATE_INFO,
203203
crate::functions::MUST_USE_UNIT_INFO,
204204
crate::functions::NOT_UNSAFE_PTR_ARG_DEREF_INFO,
205+
crate::functions::REF_OPTION_INFO,
205206
crate::functions::RENAMED_FUNCTION_PARAMS_INFO,
206207
crate::functions::RESULT_LARGE_ERR_INFO,
207208
crate::functions::RESULT_UNIT_ERR_INFO,

src/tools/clippy/clippy_lints/src/default_numeric_fallback.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ impl<'a, 'tcx> NumericFallbackVisitor<'a, 'tcx> {
119119
}
120120
}
121121

122-
impl<'a, 'tcx> Visitor<'tcx> for NumericFallbackVisitor<'a, 'tcx> {
122+
impl<'tcx> Visitor<'tcx> for NumericFallbackVisitor<'_, 'tcx> {
123123
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
124124
match &expr.kind {
125125
ExprKind::Block(

0 commit comments

Comments
 (0)