Skip to content

Lint unused parentheses around method receiver#151986

Draft
chenyukang wants to merge 1 commit intorust-lang:mainfrom
chenyukang:yukang-fix-151985-unused-parens-method-receiver
Draft

Lint unused parentheses around method receiver#151986
chenyukang wants to merge 1 commit intorust-lang:mainfrom
chenyukang:yukang-fix-151985-unused-parens-method-receiver

Conversation

@chenyukang
Copy link
Member

@chenyukang chenyukang commented Feb 2, 2026

Extends the unused_parens lint to detect unnecessary parentheses around method receivers like (x).method().

Fixes #151985

@rustbot
Copy link
Collaborator

rustbot commented Feb 2, 2026

compiler-builtins is developed in its own repository. If possible, consider making this change to rust-lang/compiler-builtins instead.

cc @tgross35

@rustbot rustbot added the A-compiler-builtins Area: compiler-builtins (https://github.com/rust-lang/compiler-builtins) label Feb 2, 2026
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Feb 2, 2026
@rustbot
Copy link
Collaborator

rustbot commented Feb 2, 2026

r? @jieyouxu

rustbot has assigned @jieyouxu.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot
Copy link
Collaborator

rustbot commented Feb 2, 2026

⚠️ Warning ⚠️

  • There are issue links (such as #123) in the commit messages of the following commits.
    Please move them to the PR description, to avoid spamming the issues with references to the commit, and so this bot can automatically canonicalize them to avoid issues with subtree.

@chenyukang chenyukang closed this Feb 2, 2026
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 2, 2026
@chenyukang chenyukang reopened this Feb 2, 2026
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 2, 2026
@chenyukang chenyukang marked this pull request as draft February 2, 2026 08:36
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 2, 2026
@chenyukang chenyukang force-pushed the yukang-fix-151985-unused-parens-method-receiver branch from c1b3bf0 to c6acedc Compare February 2, 2026 08:39
},
alloc,
));
let uninit_ptr: NonNull<_> = (unsafe { &mut *uninit_raw_ptr }).into();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems better add parens for unsafe block?

@rust-log-analyzer

This comment has been minimized.

@chenyukang chenyukang force-pushed the yukang-fix-151985-unused-parens-method-receiver branch from c6acedc to 89085e5 Compare February 2, 2026 08:57
@rust-log-analyzer

This comment has been minimized.

@chenyukang chenyukang force-pushed the yukang-fix-151985-unused-parens-method-receiver branch from 926cf7d to dc54301 Compare February 2, 2026 09:58
@rust-log-analyzer

This comment has been minimized.

@chenyukang chenyukang force-pushed the yukang-fix-151985-unused-parens-method-receiver branch from dc54301 to 5733cfa Compare February 2, 2026 11:06
@rust-log-analyzer

This comment has been minimized.

@chenyukang chenyukang force-pushed the yukang-fix-151985-unused-parens-method-receiver branch from 5733cfa to 39cd80f Compare February 2, 2026 11:36
@rust-log-analyzer

This comment has been minimized.

@chenyukang chenyukang force-pushed the yukang-fix-151985-unused-parens-method-receiver branch from 39cd80f to 823f304 Compare February 2, 2026 12:10
@rust-log-analyzer

This comment has been minimized.

@chenyukang chenyukang force-pushed the yukang-fix-151985-unused-parens-method-receiver branch from 823f304 to 4a5a8e7 Compare February 2, 2026 12:51
@rustbot rustbot added the T-rust-analyzer Relevant to the rust-analyzer team, which will review and decide on the PR/issue. label Feb 2, 2026
@rust-log-analyzer

This comment has been minimized.

@chenyukang chenyukang force-pushed the yukang-fix-151985-unused-parens-method-receiver branch from 4a5a8e7 to d657d00 Compare February 2, 2026 13:32
@rust-log-analyzer

This comment has been minimized.

@chenyukang chenyukang force-pushed the yukang-fix-151985-unused-parens-method-receiver branch from d657d00 to d18d47c Compare February 2, 2026 14:24
@rust-log-analyzer

This comment has been minimized.

@chenyukang chenyukang force-pushed the yukang-fix-151985-unused-parens-method-receiver branch from d18d47c to 885e44e Compare February 5, 2026 12:52
@rust-log-analyzer
Copy link
Collaborator

The job aarch64-gnu-llvm-20-1 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
test [ui] tests/ui/asm/aarch64/may_unwind.rs ... ok
test [ui] tests/ui/asm/aarch64/type-check-2-2.rs ... ok
test [ui] tests/ui/asm/aarch64/type-check-2.rs ... ok
test [ui] tests/ui/asm/aarch64/type-f16.rs ... ok
test [ui] tests/ui/asm/aarch64v8r.rs#hf ... ok
test [ui] tests/ui/asm/aarch64/type-check-3.rs ... ok
test [ui] tests/ui/asm/aarch64/sym.rs ... ok
test [ui] tests/ui/asm/aarch64v8r.rs#r82 ... ok
test [ui] tests/ui/asm/arm-low-dreg.rs ... ok
test [ui] tests/ui/asm/asm-with-nested-closure.rs ... ok
test [ui] tests/ui/asm/binary_asm_labels.rs ... ignored, only executed when the architecture is x86_64
test [ui] tests/ui/asm/aarch64v8r.rs#sf ... ok
test [ui] tests/ui/asm/cfg-parse-error.rs ... ok
test [ui] tests/ui/asm/cfg.rs#reva ... ignored, only executed when the architecture is x86_64
test [ui] tests/ui/asm/cfg.rs#revb ... ignored, only executed when the architecture is x86_64
test [ui] tests/ui/asm/bad-template.rs#aarch64 ... ok
test [ui] tests/ui/asm/conditionally-sized-ptr-fail.rs ... ok
---
test [ui] tests/ui/extern/issue-64655-extern-rust-must-allow-unwind.rs#fat2 ... ok
test [ui] tests/ui/extern/issue-64655-extern-rust-must-allow-unwind.rs#fat3 ... ok
test [ui] tests/ui/extern/issue-80074.rs ... ok
test [ui] tests/ui/extern/issue-95829.rs ... ok
test [ui] tests/ui/extern/lgamma-linkage.rs ... ok
test [ui] tests/ui/extern/no-mangle-associated-fn.rs ... ok
test [ui] tests/ui/extern/issue-64655-extern-rust-must-allow-unwind.rs#thin1 ... ok
test [ui] tests/ui/extern/not-in-block.rs ... ok
test [ui] tests/ui/extern/unsized-extern-derefmove.rs ... ok
test [ui] tests/ui/feature-gates/allow-features-empty.rs ... ok
---
test [ui] tests/ui/imports/ambiguous-9.rs ... ok
test [ui] tests/ui/imports/ambiguous-import-visibility-module.rs ... ok
test [ui] tests/ui/imports/ambiguous-glob-vs-expanded-extern.rs ... ok
test [ui] tests/ui/imports/ambiguous-8.rs ... ok
test [ui] tests/ui/imports/ambiguous-panic-glob-vs-multiouter.rs ... ok
test [ui] tests/ui/imports/ambiguous-panic-globvsglob.rs ... ok
test [ui] tests/ui/imports/ambiguous-panic-no-implicit-prelude.rs ... ok
test [ui] tests/ui/imports/ambiguous-panic-non-prelude-core-glob.rs ... ok
test [ui] tests/ui/imports/ambiguous-panic-non-prelude-std-glob.rs ... ok
test [ui] tests/ui/imports/ambiguous-panic-pick-core.rs ... ok
test [ui] tests/ui/imports/ambiguous-panic-pick-std.rs ... ok
---

+ error[E0433]: failed to resolve: use of undeclared type `Float`
+   --> $DIR/unused-parens-method-receiver-issue-151985.rs:28:14
+    |
+ LL |     let _ = (Float::MIN_POSITIVE).midpoint(-Float::MAX);
+    |              ^^^^^ use of undeclared type `Float`
+    |
+ help: consider importing this struct
+    |
+ LL + use std::mem::type_info::Float;
+    |
+ 
+ error[E0433]: failed to resolve: use of undeclared type `Float`
+   --> $DIR/unused-parens-method-receiver-issue-151985.rs:28:45
+    |
+ LL |     let _ = (Float::MIN_POSITIVE).midpoint(-Float::MAX);
+    |                                             ^^^^^ use of undeclared type `Float`
+    |
+ help: consider importing this struct
+    |
+ LL + use std::mem::type_info::Float;
+    |
+ 
1 error: unnecessary parentheses around method receiver
2   --> $DIR/unused-parens-method-receiver-issue-151985.rs:13:5
3    |

15 LL +     x.method();
16    |
17 
- error: aborting due to 1 previous error
+ error: unnecessary parentheses around method receiver
+   --> $DIR/unused-parens-method-receiver-issue-151985.rs:28:13
+    |
+ LL |     let _ = (Float::MIN_POSITIVE).midpoint(-Float::MAX);
+    |             ^                   ^
+    |
+ help: remove these parentheses
+    |
+ LL -     let _ = (Float::MIN_POSITIVE).midpoint(-Float::MAX);
+ LL +     let _ = Float::MIN_POSITIVE.midpoint(-Float::MAX);
+    |
19 
+ error: aborting due to 4 previous errors
+ 
+ For more information about this error, try `rustc --explain E0433`.
---
-   --> /checkout/tests/ui/lint/unused-parens-method-receiver-issue-151985.rs:28:13
+ error[E0433]: failed to resolve: use of undeclared type `Float`
+   --> $DIR/unused-parens-method-receiver-issue-151985.rs:28:14
+    |
+ LL |     let _ = (Float::MIN_POSITIVE).midpoint(-Float::MAX);
+    |              ^^^^^ use of undeclared type `Float`
+    |
+ help: consider importing this struct
+    |
+ LL + use std::mem::type_info::Float;
+    |
+ 
+ error[E0433]: failed to resolve: use of undeclared type `Float`
+   --> $DIR/unused-parens-method-receiver-issue-151985.rs:28:45
+    |
+ LL |     let _ = (Float::MIN_POSITIVE).midpoint(-Float::MAX);
+    |                                             ^^^^^ use of undeclared type `Float`
+    |
+ help: consider importing this struct
+    |
+ LL + use std::mem::type_info::Float;
+    |
+ 
+ error: unnecessary parentheses around method receiver
+   --> $DIR/unused-parens-method-receiver-issue-151985.rs:28:13
+    |
+ LL |     let _ = (Float::MIN_POSITIVE).midpoint(-Float::MAX);
+    |             ^                   ^
+    |
+ help: remove these parentheses
+    |
+ LL -     let _ = (Float::MIN_POSITIVE).midpoint(-Float::MAX);
+ LL +     let _ = Float::MIN_POSITIVE.midpoint(-Float::MAX);
+    |
+ error: aborting due to 4 previous errors
+ 
+ For more information about this error, try `rustc --explain E0433`.

---
5 struct Thing;
6 impl Thing {
7     fn method(self) {}

13     x.method(); //~ ERROR unnecessary parentheses around method receiver
14 
15     // Necessary parens - should NOT warn
-     let _ = (1..10).sum::<i32>();           // Range expression
-     let _ = (1_i32 + 2).abs();               // Binary expression
-     let _ = (-1_i32).abs();                  // Unary expression
-     let _ = (true as i32).abs();             // Cast expression
-     let _ = (&42).clone();                   // AddrOf expression
-     let _ = (&mut 42).clone();               // AddrOf mut expression
+     let _ = (1..10).sum::<i32>(); // Range expression
+     let _ = (1_i32 + 2).abs(); // Binary expression
+     let _ = (-1_i32).abs(); // Unary expression
+     let _ = (true as i32).abs(); // Cast expression
+     let _ = (&42).clone(); // AddrOf expression
+     let _ = (&mut 42).clone(); // AddrOf mut expression
22 
23     // Block expressions - should NOT warn
-     let _ = ({ 1_i32 }).abs();               // Block expression
+     let _ = ({ 1_i32 }).abs(); // Block expression
25     let _ = (unsafe { std::mem::zeroed::<i32>() }).abs(); // Unsafe block expression
+ 
+     // lint for path with multiple segments
+     let _ = Float::MIN_POSITIVE.midpoint(-Float::MAX);
26 }
27 


The actual fixed differed from the expected fixed
To update references, rerun the tests and pass the `--bless` flag
To only update this specific test, also pass `--test-args lint/unused-parens-method-receiver-issue-151985.rs`

error: 2 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/lint/unused-parens-method-receiver-issue-151985.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2" "--target=aarch64-unknown-linux-gnu" "--check-cfg" "cfg(test,FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/lint/unused-parens-method-receiver-issue-151985" "-A" "unused" "-W" "unused_attributes" "-A" "internal_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/aarch64-unknown-linux-gnu/native/rust-test-helpers"
stdout: none
--- stderr -------------------------------
error[E0433]: failed to resolve: use of undeclared type `Float`
##[error]  --> /checkout/tests/ui/lint/unused-parens-method-receiver-issue-151985.rs:28:14
   |
LL |     let _ = (Float::MIN_POSITIVE).midpoint(-Float::MAX);
   |              ^^^^^ use of undeclared type `Float`
   |
help: consider importing this struct
   |
LL + use std::mem::type_info::Float;
   |

error[E0433]: failed to resolve: use of undeclared type `Float`
##[error]  --> /checkout/tests/ui/lint/unused-parens-method-receiver-issue-151985.rs:28:45
   |
LL |     let _ = (Float::MIN_POSITIVE).midpoint(-Float::MAX);
   |                                             ^^^^^ use of undeclared type `Float`
   |
help: consider importing this struct
   |
LL + use std::mem::type_info::Float;
   |

error: unnecessary parentheses around method receiver
##[error]  --> /checkout/tests/ui/lint/unused-parens-method-receiver-issue-151985.rs:13:5
   |
LL |     (x).method(); //~ ERROR unnecessary parentheses around method receiver
   |     ^ ^
   |
note: the lint level is defined here
  --> /checkout/tests/ui/lint/unused-parens-method-receiver-issue-151985.rs:3:9
   |
LL | #![deny(unused_parens)]
   |         ^^^^^^^^^^^^^
help: remove these parentheses
   |
LL -     (x).method(); //~ ERROR unnecessary parentheses around method receiver
LL +     x.method(); //~ ERROR unnecessary parentheses around method receiver
   |

error: unnecessary parentheses around method receiver
##[error]  --> /checkout/tests/ui/lint/unused-parens-method-receiver-issue-151985.rs:28:13
   |
LL |     let _ = (Float::MIN_POSITIVE).midpoint(-Float::MAX);
   |             ^                   ^
   |
help: remove these parentheses
   |
LL -     let _ = (Float::MIN_POSITIVE).midpoint(-Float::MAX);
LL +     let _ = Float::MIN_POSITIVE.midpoint(-Float::MAX);
   |

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0433`.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-compiler-builtins Area: compiler-builtins (https://github.com/rust-lang/compiler-builtins) S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. T-rust-analyzer Relevant to the rust-analyzer team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

unused_parens does not lint parentheses around method receiver

4 participants