Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7c1793c

Browse files
authoredFeb 20, 2025
Unrolled build for rust-lang#136923
Rollup merge of rust-lang#136923 - samueltardieu:push-vxxqvqwspssv, r=davidtwco Lint `#[must_use]` attributes applied to methods in trait impls The `#[must_use]` attribute has no effect when applied to methods in trait implementations. This PR adds it to the unused `#[must_use]` lint, and cleans the extra attributes in portable-simd and Clippy.
2 parents f280acf + e639e88 commit 7c1793c

File tree

11 files changed

+55
-62
lines changed

11 files changed

+55
-62
lines changed
 

‎compiler/rustc_passes/src/check_attr.rs

+29-18
Original file line numberDiff line numberDiff line change
@@ -1431,37 +1431,48 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
14311431

14321432
/// Warns against some misuses of `#[must_use]`
14331433
fn check_must_use(&self, hir_id: HirId, attr: &Attribute, target: Target) {
1434-
if !matches!(
1434+
if matches!(
14351435
target,
14361436
Target::Fn
14371437
| Target::Enum
14381438
| Target::Struct
14391439
| Target::Union
1440-
| Target::Method(_)
1440+
| Target::Method(MethodKind::Trait { body: false } | MethodKind::Inherent)
14411441
| Target::ForeignFn
14421442
// `impl Trait` in return position can trip
14431443
// `unused_must_use` if `Trait` is marked as
14441444
// `#[must_use]`
14451445
| Target::Trait
14461446
) {
1447-
let article = match target {
1448-
Target::ExternCrate
1449-
| Target::Enum
1450-
| Target::Impl
1451-
| Target::Expression
1452-
| Target::Arm
1453-
| Target::AssocConst
1454-
| Target::AssocTy => "an",
1455-
_ => "a",
1456-
};
1447+
return;
1448+
}
14571449

1458-
self.tcx.emit_node_span_lint(
1459-
UNUSED_ATTRIBUTES,
1460-
hir_id,
1461-
attr.span,
1462-
errors::MustUseNoEffect { article, target },
1463-
);
1450+
// `#[must_use]` can be applied to a trait method definition with a default body
1451+
if let Target::Method(MethodKind::Trait { body: true }) = target
1452+
&& let parent_def_id = self.tcx.hir().get_parent_item(hir_id).def_id
1453+
&& let containing_item = self.tcx.hir().expect_item(parent_def_id)
1454+
&& let hir::ItemKind::Trait(..) = containing_item.kind
1455+
{
1456+
return;
14641457
}
1458+
1459+
let article = match target {
1460+
Target::ExternCrate
1461+
| Target::Enum
1462+
| Target::Impl
1463+
| Target::Expression
1464+
| Target::Arm
1465+
| Target::AssocConst
1466+
| Target::AssocTy => "an",
1467+
_ => "a",
1468+
};
1469+
1470+
self.tcx.emit_node_span_lint(
1471+
UNUSED_ATTRIBUTES,
1472+
hir_id,
1473+
attr.span,
1474+
errors::MustUseNoEffect { article, target },
1475+
);
14651476
}
14661477

14671478
/// Checks if `#[must_not_suspend]` is applied to a struct, enum, union, or trait.

‎library/portable-simd/crates/core_simd/src/masks.rs

-13
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,6 @@ where
401401
LaneCount<N>: SupportedLaneCount,
402402
{
403403
#[inline]
404-
#[must_use = "method returns a defaulted mask with all elements set to false (0)"]
405404
fn default() -> Self {
406405
Self::splat(false)
407406
}
@@ -413,7 +412,6 @@ where
413412
LaneCount<N>: SupportedLaneCount,
414413
{
415414
#[inline]
416-
#[must_use = "method returns a new bool and does not mutate the original value"]
417415
fn eq(&self, other: &Self) -> bool {
418416
self.0 == other.0
419417
}
@@ -425,7 +423,6 @@ where
425423
LaneCount<N>: SupportedLaneCount,
426424
{
427425
#[inline]
428-
#[must_use = "method returns a new Ordering and does not mutate the original value"]
429426
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
430427
self.0.partial_cmp(&other.0)
431428
}
@@ -451,7 +448,6 @@ where
451448
{
452449
type Output = Self;
453450
#[inline]
454-
#[must_use = "method returns a new mask and does not mutate the original value"]
455451
fn bitand(self, rhs: Self) -> Self {
456452
Self(self.0 & rhs.0)
457453
}
@@ -464,7 +460,6 @@ where
464460
{
465461
type Output = Self;
466462
#[inline]
467-
#[must_use = "method returns a new mask and does not mutate the original value"]
468463
fn bitand(self, rhs: bool) -> Self {
469464
self & Self::splat(rhs)
470465
}
@@ -477,7 +472,6 @@ where
477472
{
478473
type Output = Mask<T, N>;
479474
#[inline]
480-
#[must_use = "method returns a new mask and does not mutate the original value"]
481475
fn bitand(self, rhs: Mask<T, N>) -> Mask<T, N> {
482476
Mask::splat(self) & rhs
483477
}
@@ -490,7 +484,6 @@ where
490484
{
491485
type Output = Self;
492486
#[inline]
493-
#[must_use = "method returns a new mask and does not mutate the original value"]
494487
fn bitor(self, rhs: Self) -> Self {
495488
Self(self.0 | rhs.0)
496489
}
@@ -503,7 +496,6 @@ where
503496
{
504497
type Output = Self;
505498
#[inline]
506-
#[must_use = "method returns a new mask and does not mutate the original value"]
507499
fn bitor(self, rhs: bool) -> Self {
508500
self | Self::splat(rhs)
509501
}
@@ -516,7 +508,6 @@ where
516508
{
517509
type Output = Mask<T, N>;
518510
#[inline]
519-
#[must_use = "method returns a new mask and does not mutate the original value"]
520511
fn bitor(self, rhs: Mask<T, N>) -> Mask<T, N> {
521512
Mask::splat(self) | rhs
522513
}
@@ -529,7 +520,6 @@ where
529520
{
530521
type Output = Self;
531522
#[inline]
532-
#[must_use = "method returns a new mask and does not mutate the original value"]
533523
fn bitxor(self, rhs: Self) -> Self::Output {
534524
Self(self.0 ^ rhs.0)
535525
}
@@ -542,7 +532,6 @@ where
542532
{
543533
type Output = Self;
544534
#[inline]
545-
#[must_use = "method returns a new mask and does not mutate the original value"]
546535
fn bitxor(self, rhs: bool) -> Self::Output {
547536
self ^ Self::splat(rhs)
548537
}
@@ -555,7 +544,6 @@ where
555544
{
556545
type Output = Mask<T, N>;
557546
#[inline]
558-
#[must_use = "method returns a new mask and does not mutate the original value"]
559547
fn bitxor(self, rhs: Mask<T, N>) -> Self::Output {
560548
Mask::splat(self) ^ rhs
561549
}
@@ -568,7 +556,6 @@ where
568556
{
569557
type Output = Mask<T, N>;
570558
#[inline]
571-
#[must_use = "method returns a new mask and does not mutate the original value"]
572559
fn not(self) -> Self::Output {
573560
Self(!self.0)
574561
}

‎library/portable-simd/crates/core_simd/src/masks/full_masks.rs

-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ where
2121
LaneCount<N>: SupportedLaneCount,
2222
{
2323
#[inline]
24-
#[must_use = "method returns a new mask and does not mutate the original value"]
2524
fn clone(&self) -> Self {
2625
*self
2726
}
@@ -252,7 +251,6 @@ where
252251
{
253252
type Output = Self;
254253
#[inline]
255-
#[must_use = "method returns a new mask and does not mutate the original value"]
256254
fn bitand(self, rhs: Self) -> Self {
257255
// Safety: `self` is an integer vector
258256
unsafe { Self(core::intrinsics::simd::simd_and(self.0, rhs.0)) }
@@ -266,7 +264,6 @@ where
266264
{
267265
type Output = Self;
268266
#[inline]
269-
#[must_use = "method returns a new mask and does not mutate the original value"]
270267
fn bitor(self, rhs: Self) -> Self {
271268
// Safety: `self` is an integer vector
272269
unsafe { Self(core::intrinsics::simd::simd_or(self.0, rhs.0)) }
@@ -280,7 +277,6 @@ where
280277
{
281278
type Output = Self;
282279
#[inline]
283-
#[must_use = "method returns a new mask and does not mutate the original value"]
284280
fn bitxor(self, rhs: Self) -> Self {
285281
// Safety: `self` is an integer vector
286282
unsafe { Self(core::intrinsics::simd::simd_xor(self.0, rhs.0)) }
@@ -294,7 +290,6 @@ where
294290
{
295291
type Output = Self;
296292
#[inline]
297-
#[must_use = "method returns a new mask and does not mutate the original value"]
298293
fn not(self) -> Self::Output {
299294
Self::splat(true) ^ self
300295
}

‎library/portable-simd/crates/core_simd/src/ops.rs

-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ macro_rules! for_base_types {
135135
type Output = $out;
136136

137137
#[inline]
138-
#[must_use = "operator returns a new vector without mutating the inputs"]
139138
// TODO: only useful for int Div::div, but we hope that this
140139
// will essentially always get inlined anyway.
141140
#[track_caller]

‎library/portable-simd/crates/core_simd/src/ops/deref.rs

-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ macro_rules! deref_lhs {
1818
type Output = Simd<T, N>;
1919

2020
#[inline]
21-
#[must_use = "operator returns a new vector without mutating the inputs"]
2221
fn $call(self, rhs: $simd) -> Self::Output {
2322
(*self).$call(rhs)
2423
}
@@ -39,7 +38,6 @@ macro_rules! deref_rhs {
3938
type Output = Simd<T, N>;
4039

4140
#[inline]
42-
#[must_use = "operator returns a new vector without mutating the inputs"]
4341
fn $call(self, rhs: &$simd) -> Self::Output {
4442
self.$call(*rhs)
4543
}
@@ -71,7 +69,6 @@ macro_rules! deref_ops {
7169
type Output = $simd;
7270

7371
#[inline]
74-
#[must_use = "operator returns a new vector without mutating the inputs"]
7572
fn $call(self, rhs: &'rhs $simd) -> Self::Output {
7673
(*self).$call(*rhs)
7774
}

‎library/portable-simd/crates/core_simd/src/ops/unary.rs

-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ macro_rules! neg {
1111
type Output = Self;
1212

1313
#[inline]
14-
#[must_use = "operator returns a new vector without mutating the input"]
1514
fn neg(self) -> Self::Output {
1615
// Safety: `self` is a signed vector
1716
unsafe { core::intrinsics::simd::simd_neg(self) }
@@ -46,7 +45,6 @@ macro_rules! not {
4645
type Output = Self;
4746

4847
#[inline]
49-
#[must_use = "operator returns a new vector without mutating the input"]
5048
fn not(self) -> Self::Output {
5149
self ^ (Simd::splat(!(0 as $scalar)))
5250
}

‎library/portable-simd/crates/core_simd/src/simd/num/float.rs

-1
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,6 @@ macro_rules! impl_trait {
371371
}
372372

373373
#[inline]
374-
#[must_use = "method returns a new mask and does not mutate the original value"]
375374
fn is_normal(self) -> Self::Mask {
376375
!(self.abs().simd_eq(Self::splat(0.0)) | self.is_nan() | self.is_subnormal() | self.is_infinite())
377376
}

‎src/tools/clippy/clippy_lints/src/infinite_iter.rs

-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ impl Finiteness {
9494
}
9595

9696
impl From<bool> for Finiteness {
97-
#[must_use]
9897
fn from(b: bool) -> Self {
9998
if b { Infinite } else { Finite }
10099
}

‎src/tools/clippy/clippy_utils/src/consts.rs

-3
Original file line numberDiff line numberDiff line change
@@ -351,21 +351,18 @@ pub enum FullInt {
351351
}
352352

353353
impl PartialEq for FullInt {
354-
#[must_use]
355354
fn eq(&self, other: &Self) -> bool {
356355
self.cmp(other) == Ordering::Equal
357356
}
358357
}
359358

360359
impl PartialOrd for FullInt {
361-
#[must_use]
362360
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
363361
Some(self.cmp(other))
364362
}
365363
}
366364

367365
impl Ord for FullInt {
368-
#[must_use]
369366
fn cmp(&self, other: &Self) -> Ordering {
370367
use FullInt::{S, U};
371368

‎tests/ui/lint/unused/unused_attributes-must_use.rs

+5
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ trait Use {
7979
#[must_use] //~ ERROR `#[must_use]` has no effect
8080
impl Use for () {
8181
type AssocTy = ();
82+
83+
#[must_use] //~ ERROR `#[must_use]` has no effect
84+
fn get_four(&self) -> usize {
85+
4
86+
}
8287
}
8388

8489
#[must_use] //~ ERROR `#[must_use]` has no effect
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Failed to load comments.