Skip to content

Commit

Permalink
fix incorrect suggestions related to parentheses in needless_return
Browse files Browse the repository at this point in the history
  • Loading branch information
lapla-cogito committed Mar 5, 2025
1 parent a8b1782 commit 1cd7022
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 58 deletions.
2 changes: 1 addition & 1 deletion clippy_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3552,7 +3552,7 @@ pub fn is_block_like(expr: &Expr<'_>) -> bool {
pub fn binary_expr_needs_parentheses(expr: &Expr<'_>) -> bool {
fn contains_block(expr: &Expr<'_>, is_operand: bool) -> bool {
match expr.kind {
ExprKind::Binary(_, lhs, _) => contains_block(lhs, true),
ExprKind::Binary(_, lhs, _) | ExprKind::Cast(lhs, _) => contains_block(lhs, true),
_ if is_block_like(expr) => is_operand,
_ => false,
}
Expand Down
12 changes: 11 additions & 1 deletion tests/ui/needless_return.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
clippy::single_match,
clippy::needless_bool,
clippy::equatable_if_let,
clippy::needless_else
clippy::needless_else,
clippy::missing_safety_doc
)]
#![warn(clippy::needless_return)]

Expand Down Expand Up @@ -442,3 +443,12 @@ fn b(x: Option<u8>) -> Option<u8> {
},
}
}

unsafe fn todo() -> *const u8 {
todo!()
}

pub unsafe fn issue_12157() -> *const i32 {
(unsafe { todo() } as *const i32)
//~^ needless_return
}
12 changes: 11 additions & 1 deletion tests/ui/needless_return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
clippy::single_match,
clippy::needless_bool,
clippy::equatable_if_let,
clippy::needless_else
clippy::needless_else,
clippy::missing_safety_doc
)]
#![warn(clippy::needless_return)]

Expand Down Expand Up @@ -451,3 +452,12 @@ fn b(x: Option<u8>) -> Option<u8> {
},
}
}

unsafe fn todo() -> *const u8 {
todo!()
}

pub unsafe fn issue_12157() -> *const i32 {
return unsafe { todo() } as *const i32;
//~^ needless_return
}
Loading

0 comments on commit 1cd7022

Please sign in to comment.