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

False negative: borrow of moved value #5148

Open
j-delaney opened this issue Mar 29, 2020 · 5 comments
Open

False negative: borrow of moved value #5148

j-delaney opened this issue Mar 29, 2020 · 5 comments
Assignees
Labels
improvement subsystem::code insight General label for issues related to code understanding: highlighting, completion, annotation, etc.

Comments

@j-delaney
Copy link

Environment

  • IntelliJ Rust plugin version: 0.2.118.2171-193
  • Rust toolchain version: rustc 1.42.0 (b8cedc004 2020-03-09)
  • IDE name and version: IntelliJ IDEA 2019.3.4 (Ultimate Edition)
  • Operating system: macOS 10.14.5

Problem description

When attempting to borrow a moved value, it should identify this issue in the IDE and alert the user.

Steps to reproduce

fn main() {
    let s1 = String::from("hello");
    let s2 = s1;

    println!("{}", s1);
}

With cargo check this gives:

error[E0382]: borrow of moved value: `s1`
 --> src/main.rs:5:20
  |
2 |     let s1 = String::from("hello");
  |         -- move occurs because `s1` has type `std::string::String`, which does not implement the `Copy` trait
3 |     let s2 = s1;
  |              -- value moved here
4 | 
5 |     println!("{}", s1);
  |                    ^^ value borrowed here after move

but it does not error within IDEA

@j-delaney
Copy link
Author

This also doesn't appropriately flag an error when taking ownership via a function call:

fn main() {
    let s = String::from("");
    take_ownership(s);
    println!("{}", s); // This should be highlighted as an error
}

fn take_ownership(s: String) {}

@aloucks
Copy link

aloucks commented Mar 29, 2020

Possibly related false positive on move:

#4850 (comment)

@Undin Undin added bug subsystem::code insight General label for issues related to code understanding: highlighting, completion, annotation, etc. labels Mar 30, 2020
@Undin
Copy link
Member

Undin commented Mar 30, 2020

Possibly related false positive on move:

#4850 (comment)

No, they are not related

@Undin
Copy link
Member

Undin commented Mar 30, 2020

@ortem Looks like Borrow Checker inspection doesn't take into account macro arguments

@artemmukhin
Copy link
Member

artemmukhin commented Mar 30, 2020

@Undin Yes, I disabled Borrow Checker inspection on non-physical (e.g. expanded from macros) elements in this commit 70b6d1e because such elements are not stored in a given user file and cannot be highlighted directly. Note that our move analysis (which is used by Borrow checker inspection) takes macros into account and knows about the move errors in macro calls, but the inspection just ignores such cases. The inspection works well when the first move is inside a macro and the second move is inside an expression:

macro_rules! consume { ($e:expr) => ($e;); }

struct S;

fn main() {
    let s = S;
    consume!(s);
    s; // highlighted move error
}

So it would be great to use the information from move analysis to highlight corresponding macro arguments as the cause of the error. I've opened the corresponding issue: #5152

However, the cases described by @j-delaney relate to borrow analysis, not move analysis, so they are not supported yet in our plugin. Our Borrow Checker inspection only checks moves (and also mutations) and doesn't know about Borrow of moved value error. E.g. currently it doesn't work even in this case:

struct S;

fn loan(s: &S) {}

fn main() {
    let s1 = S;
    let s2 = s1;
    loan(&s1); // oops, no error highlighted
}

But I think such primitive cases can be highlighted with Use of moved value, without implementing the proper borrow analysis. So I'll try to find a simple way to implement it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
improvement subsystem::code insight General label for issues related to code understanding: highlighting, completion, annotation, etc.
Projects
None yet
Development

No branches or pull requests

4 participants