Skip to content
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

uninit_vec: fix false positive with set_len(0) #9519

Merged
merged 1 commit into from
Sep 23, 2022

Conversation

alessandrod
Copy link
Contributor

@alessandrod alessandrod commented Sep 22, 2022

set_len(0) does not create uninitialized elements. Fixes a false positive with the following pattern:

fn copy_slice_into_vec(dst: &mut Vec<u8>, src: &[u8]) {
    dst.reserve(src.len().saturating_sub(dst.len()));
    unsafe {
        dst.set_len(0);
        std::ptr::copy_nonoverlapping(src.as_ptr(), dst.as_mut_ptr(), src.len());
        dst.set_len(src.len());
    }
}

zulip thread: https://rust-lang.zulipchat.com/#narrow/stream/257328-clippy/topic/uninit_vec.20and.20set_len.280.29

changelog: FP: [uninit_vec]: No longer lints Vec::set_len(0)

@rust-highfive
Copy link

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @llogiq (or someone else) soon.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Sep 22, 2022
@@ -211,9 +212,12 @@ fn extract_set_len_self<'tcx>(cx: &LateContext<'_>, expr: &'tcx Expr<'_>) -> Opt
}
});
match expr.kind {
ExprKind::MethodCall(path, self_expr, [_], _) => {
ExprKind::MethodCall(path, self_expr, args, _) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
ExprKind::MethodCall(path, self_expr, args, _) => {
ExprKind::MethodCall(path, self_expr, [arg], _) => {

You can match the single arg directly, because all set_len calls have exactly one argument. This will save you an if let in is_literal_zero.

@llogiq
Copy link
Contributor

llogiq commented Sep 23, 2022

Thank you for the PR. I have only a very small style suggestion, otherwise this looks mergeworthy.

set_len(0) does not create uninitialized elements. Fixes a false positive with
the following pattern:

    fn copy_slice_into_vec(dst: &mut Vec<u8>, src: &[u8]) {
        dst.reserve(src.len().saturating_sub(dst.len()));
        unsafe {
            dst.set_len(0);
            std::ptr::copy_nonoverlapping(src.as_ptr(), dst.as_mut_ptr(), src.len());
            dst.set_len(src.len());
        }
    }
@alessandrod
Copy link
Contributor Author

Thank you for the PR. I have only a very small style suggestion, otherwise this looks mergeworthy.

Thanks for reviewing!

@llogiq
Copy link
Contributor

llogiq commented Sep 23, 2022

Thank you! @bors r+

@bors
Copy link
Collaborator

bors commented Sep 23, 2022

📌 Commit 49319b4 has been approved by llogiq

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented Sep 23, 2022

⌛ Testing commit 49319b4 with merge dc14531...

@bors
Copy link
Collaborator

bors commented Sep 23, 2022

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: llogiq
Pushing dc14531 to master...

@bors bors merged commit dc14531 into rust-lang:master Sep 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants