-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New lint: Suggest ptr.add([usize])
over ptr.offset([usize] as isize)
.
#3104
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
New lint: Suggest ptr.add([usize])
over ptr.offset([usize] as isize)
.
#3104
Conversation
f8b2da3
to
e0c7eb7
Compare
/// ``` | ||
declare_clippy_lint! { | ||
pub PTR_OFFSET_WITH_CAST, | ||
style, |
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.
Is this the right category?
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.
It would fit the complexity category. Feels less like a stylistic choice, more like in the scope of "use x.is_empty()
instead of x.len() == 0
.
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.
…e)`. First part of rust-lang#3047.
e0c7eb7
to
5ebae01
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.
That split into many functions <3
Just a few small things. Also: you can probably easily extend your code to also catch wrapping_offset
doing the same thing
cx: &lint::LateContext<'a, 'tcx>, | ||
expr: &hir::Expr, | ||
) -> bool { | ||
cx.tables.expr_ty(expr).sty == ty::TyKind::Uint(ast::UintTy::Usize) |
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 can use cx.tcx.types.usize
to compare directly with the result of expr_ty
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.
cx: &lint::LateContext<'a, 'tcx>, | ||
expr: &hir::Expr, | ||
) -> bool { | ||
if let ty::RawPtr(..) = cx.tables.expr_ty(expr).sty { |
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 can use the is_unsafe_ptr
method instead of destructuring
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.
utils::snippet_opt(cx, cast_lhs_expr.span) | ||
) { | ||
(Some(receiver), Some(cast_lhs)) => format!("{}.add({})", receiver, cast_lhs), | ||
_ => String::new(), |
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.
if the snippet is not obtainable, instead of reporting an empty snippet, we should not produce any suggestion at all
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.
thanks for the quick review @oli-obk! your comments should be addressed |
oh missed this, let me add it in |
} | ||
|
||
fn build_suggestion<'a, 'tcx>( | ||
cx: &lint::LateContext<'a, 'tcx>, | ||
receiver_expr: &hir::Expr, | ||
cast_lhs_expr: &hir::Expr, | ||
) -> String { | ||
) -> Option<String> { | ||
match ( | ||
utils::snippet_opt(cx, receiver_expr.span), |
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.
Now your code can be simplified to
let receiver = utils::snippet_opt(cx, receiver_expr.span)?;
let cast_lhs = utils::snippet_opt(cx, cast_lhs_expr.span)?;
Some(format!("{}.add({})", receiver, cast_lhs))
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.
oh right, ?
with Option
is now a thing 💅
travis is unhappy (just make your enum r=me with travis passing |
Travis error:
|
ed0b16a
to
f42442b
Compare
@bors r=oli-obk |
I don't think we have bors setup. we totally should though |
wasn't sure if i had bors privileges here – looks like someone else will have to approve or merge |
First part of #3047.
Could be written: