Skip to content

Variables that are only used for swapping are marked as unused #1832

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

Closed
marijnh opened this issue Feb 13, 2012 · 8 comments
Closed

Variables that are only used for swapping are marked as unused #1832

marijnh opened this issue Feb 13, 2012 · 8 comments
Milestone

Comments

@marijnh
Copy link
Contributor

marijnh commented Feb 13, 2012

This causes a warning that x is unused:

fn main() {
   let x = 10, y = 20;
   x <-> y;
   log(error, y);
}
@ghost ghost assigned catamorphism Feb 13, 2012
@catamorphism
Copy link
Contributor

Definitely a bug; will be annoying to fix, though, since this is the only thing that makes the "unused variables" analysis non-local.

@marijnh
Copy link
Contributor Author

marijnh commented Feb 13, 2012

How does it make it non-local? If you just perform the same marking of used for the LHS of the swap as you're doing for the RHS, I think that's plenty good enough.

@catamorphism
Copy link
Contributor

Well, the question is, suppose I have the following program:

fn main() {
   let x = 10, y = 20;
   x <-> y;

Where x and y get swapped, but never used. If I treat both sides of a swap the same way I would treat the RHS of an assignment, then the compiler won't print a warning for this program. Arguably that's wrong -- x and y are both swapped, but neither is actually, used.

OTOH, if others think that a conservative approximation (not warning for the above case) is acceptable, I'm ok with doing the simplest thing. Does that make sense?

@marijnh
Copy link
Contributor Author

marijnh commented Feb 14, 2012

A conservative thing that at least doesn't emit spurious warnings is good enough for me. A 'correct' analysis would be awesome, but probably not worth the extra complexity.

@catamorphism
Copy link
Contributor

I'm ok with doing the easy thing here :-)

@jruderman
Copy link
Contributor

IMO, the "unused" warning should be per-assignment, not per-variable.

fn main() {
   let x = 10;
   x = 20;
   log(error, x);
}

should warn because the first assignment to x is unused.

fn main() {
   let x = 10;
   log(error, x);
   x = 20;
}

should warn because the second assignment to x is unused. Finally, the original example,

fn main() {
   let x = 10, y = 20;
   x <-> y;
   log(error, y);
}

should warn because the second assignment to x is unused.

@marijnh
Copy link
Contributor Author

marijnh commented Feb 16, 2012

I agree, actually.

The last-use pass already has this information, and is probably easier to extend than the typestate code. We could move unused variable analysis into that pass.

@catamorphism
Copy link
Contributor

The original bug seems to have been fixed. I don't know how to add a test case for it, because there doesn't seem to be a way (in lint) to turn the unused-variable warning into an error.

As for @jruderman 's proposal, I'm going to turn that into a separate bug.

celinval pushed a commit to celinval/rust-dev that referenced this issue Jun 4, 2024
Kobzol pushed a commit to Kobzol/rust that referenced this issue Dec 30, 2024
bors pushed a commit to rust-lang-ci/rust that referenced this issue Jan 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants