-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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::read
instead of mem::replace(..., uninitialized())
#5695
Conversation
r? @phansch (rust_highfive has picked a reviewer for you, use r? to override) |
Hello, this is the draft implementation of a lint to suggest using I think it's currently a bit messy, so feel free to give me feedback so I can work on them. Thanks! |
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.
The implementation looks good to me, just two nits 👍
Apart from that I don't have much experience with unsafe rust. It would be good to have a second pair of eyes for the UI tests, so maybe r? @oli-obk
clippy_lints/src/mem_replace.rs
Outdated
fn check_replace_with_uninit(cx: &LateContext<'_, '_>, src: &Expr<'_>, expr_span: Span) { | ||
if let ExprKind::Call(ref repl_func, ref repl_args) = src.kind { | ||
fn check_replace_with_uninit(cx: &LateContext<'_, '_>, src: &Expr<'_>, dest: &Expr<'_>, expr_span: Span) { | ||
if let ExprKind::MethodCall(method_path, _, method_args) = src.kind { |
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.
This if let
can be moved down into the if_chain
to remove one level of indentation
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.
Okay, I have moved the MethodCall
check inside the if_chain
clippy_lints/src/mem_replace.rs
Outdated
if_chain! { | ||
// check if replacement is `maybe_uninit::uninit().assume_init()`: | ||
if method_path.ident.name == sym!(assume_init); | ||
if ! method_args.is_empty(); |
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 ! method_args.is_empty(); | |
if !method_args.is_empty(); |
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.
This formatting typo has been fixed
Hi @oli-obk, just want to follow up on this PR in case my previous comment got buried. I will quote my previous comment:
And this is the commit: a62ae7c Thanks! |
a62ae7c
to
8c1ee06
Compare
r? @phansch for final review |
@bors r+ thanks! |
📌 Commit 8c1ee06 has been approved by |
New lint: suggest `ptr::read` instead of `mem::replace(..., uninitialized())` resolves: #5575 changelog: - add a new test case in `tests/ui/repl_uninit.rs` to cover the case of replacing with `mem::MaybeUninit::uninit().assume_init()`. - modify the existing `MEM_REPLACE_WITH_UNINIT` when replacing with `mem::uninitialized` to suggest using `ptr::read` instead. - lint with `MEM_REPLACE_WITH_UNINIT` when replacing with `mem::MaybeUninit::uninit().assume_init()`
💔 Test failed - checks-action_test |
@bors r=phansch,oli-obk retry |
💡 This pull request was already approved, no need to approve it again.
|
📌 Commit 8c1ee06 has been approved by |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
resolves: #5575
changelog: improvements to
MEM_REPLACE_WITH_UNINIT
:tests/ui/repl_uninit.rs
to cover the case of replacing withmem::MaybeUninit::uninit().assume_init()
.MEM_REPLACE_WITH_UNINIT
when replacing withmem::uninitialized
to suggest usingptr::read
instead.MEM_REPLACE_WITH_UNINIT
when replacing withmem::MaybeUninit::uninit().assume_init()