-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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.
cd7fc83
to
471f9c5
Compare
There was a problem hiding this 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.
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))) | ||
} |
There was a problem hiding this comment.
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) => { |
There was a problem hiding this comment.
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.
|| cx | ||
.typeck_results() | ||
.expr_adjustments(expr) | ||
.iter() | ||
.any(|adj| matches!(adj.kind, Adjust::Borrow(_))) |
There was a problem hiding this comment.
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.
Closes #14640
changelog: [
unnecessary_cast
] fix extra brackets in suggestions when in macro