Skip to content

fix: unnecessary_cast suggests extra brackets when in macro #14643

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

profetia
Copy link
Contributor

@profetia profetia commented Apr 17, 2025

Closes #14640

changelog: [unnecessary_cast] fix extra brackets in suggestions when in macro

@profetia profetia marked this pull request as ready for review April 17, 2025 12:39
@rustbot
Copy link
Collaborator

rustbot commented Apr 17, 2025

r? @Jarcho

rustbot has assigned @Jarcho.
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 rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Apr 17, 2025
Copy link
Contributor

@Jarcho Jarcho left a comment

Choose a reason for hiding this comment

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

One minor nit, otherwise looks ok.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Apr 17, 2025
@profetia profetia force-pushed the issue14640 branch 2 times, most recently from cd7fc83 to 471f9c5 Compare April 22, 2025 10:48
Copy link
Contributor

@Jarcho Jarcho left a comment

Choose a reason for hiding this comment

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

Just a couple of small perf nits.

Comment on lines +162 to +172
fn is_in_allowed_macro(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
const ALLOWED_MACROS: &[Symbol] = &[
sym::format_args_macro,
sym::assert_eq_macro,
sym::debug_assert_eq_macro,
sym::assert_ne_macro,
sym::debug_assert_ne_macro,
];
matches!(expr.span.ctxt().outer_expn_data().macro_def_id, Some(def_id) if
ALLOWED_MACROS.iter().any(|&sym| cx.tcx.is_diagnostic_item(sym, def_id)))
}
Copy link
Contributor

Choose a reason for hiding this comment

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

You want to use get_diagnostic_name when checking for multiple possible diagnostic items.

// Changing `&(x as i32)` to `&x` would change the meaning of the code because the previous creates
// a reference to the temporary while the latter creates a reference to the original value.
let surrounding = match cx.tcx.parent_hir_node(expr.hir_id) {
Node::Expr(parent) if !is_in_allowed_macro(cx, parent) && is_borrow_expr(cx, parent) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Small perf nit: The macro check only needs to occur for AddrOf expressions and like it currently does the adjustment check only needs to occur for other expressions.

Comment on lines +155 to +159
|| cx
.typeck_results()
.expr_adjustments(expr)
.iter()
.any(|adj| matches!(adj.kind, Adjust::Borrow(_)))
Copy link
Contributor

Choose a reason for hiding this comment

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

Another small nit: Only the first adjustment needs to be checked. Any later borrow would have to borrow a temporary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unnecessary curve brackets in suggestion generated by [unnecessary_cast] lint
4 participants