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

Add detection of unused struct fields #14696

Merged
merged 4 commits into from Jun 10, 2014
Merged

Add detection of unused struct fields #14696

merged 4 commits into from Jun 10, 2014

Conversation

ghost
Copy link

@ghost ghost commented Jun 6, 2014

This uncovered some dead code, most notably in middle/liveness.rs, which I think suggests there must be something fishy with that part of the code.

The #[allow(dead_code)] annotations on some of the fields I am not super happy about but as I understand, marker type may disappear at some point.

@ghost ghost changed the title Dead struct fields Add detection of unused struct fields Jun 6, 2014
@thestinger
Copy link
Contributor

It would be nice to teach this to ignore the marker types. They're lang items so it's possible to detect these without too much effort. It would also make sense to ignore a struct if it has #[repr(C)].

@ghost
Copy link
Author

ghost commented Jun 6, 2014

@thestinger I think #[repr(C)] can only be used for enums.

As for markers, that does sound like a good idea! I made that change.

The warning is still a false positive for resources (locks) but I do not see a way to whitelist those other than whitelisting types implementing Drop but that would introduce some false negatives as well.

@thestinger
Copy link
Contributor

It's also going to be used for every struct requiring C layout now, but I'm unsure if that pull request landed. The RFC for that was accepted.

@thestinger
Copy link
Contributor

Ah, it didn't land - it's #14479.

@ghost ghost closed this Jun 6, 2014
@ghost ghost deleted the dead-struct-fields branch June 6, 2014 17:33
@ghost ghost restored the dead-struct-fields branch June 6, 2014 17:34
@ghost ghost reopened this Jun 6, 2014
@ghost
Copy link
Author

ghost commented Jun 6, 2014

@thestinger I see, thanks! I went ahead and I added repr(C) handling to this PR.

@huonw
Copy link
Member

huonw commented Jun 8, 2014

This needs a rebase.

@alexcrichton
Copy link
Member

I'm a little worried about how noisy our lints are becoming, it seems there are some legitimate use cases here that are being warned about and it's annoying to have to put #[allow(dead_code)] all over the place.

I think that whitelisting fields of #[repr(C)] will go a long way, but I also think another piece that could help is not warning about fields that start with an underscore (like the mutability/unused variable lints). I imagine that the amount of #[allow(dead_code)] will go down, but perhaps not all the way. I would ideally like to see 0 additions of #[allow(dead_code)] as part of this PR, but that's not necessarily a requirement per-se.

I added repr(C) handling to this PR.

Was this left out by accident? I may have missed it as well.

@huonw
Copy link
Member

huonw commented Jun 8, 2014

I also think another piece that could help is not warning about fields that start with an underscore (like the mutability/unused variable lints).

👍 mirrors the use of local-variables guards too: let _guard = lock.lock(); ....

@ghost
Copy link
Author

ghost commented Jun 8, 2014

Was this left out by accident? I may have missed it as well.

Yes, no idea how that'd happened. I couldn't even find it in my reflog!

I agree that noisy lints are bad. But I was concerned that limiting the warnings to a smaller subset of scenarios reduces their utility. In this case, though, the leading underscore rule does seem like a good solution!

Updated to include both repr(C) and leading _ changes.

@@ -254,7 +254,7 @@ pub struct TcpStream {

struct Inner {
fd: sock_t,
lock: mutex::NativeMutex,
_lock: mutex::NativeMutex,
Copy link
Member

Choose a reason for hiding this comment

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

It seems... weird that this field would be unused. Why is it creating a mutex without using it?

Copy link
Author

Choose a reason for hiding this comment

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

It is using it but not on Linux. I'll make the field compile-time conditional to non-Linux.

Copy link
Author

Choose a reason for hiding this comment

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

Okay, that's not really possible. I'll stick with an underscore, then.

@alexcrichton
Copy link
Member

Awesome work! I'm quite happy with how the leading underscore turned out.

@huonw
Copy link
Member

huonw commented Jun 8, 2014

Agreed. (Travis failed.)

@ghost
Copy link
Author

ghost commented Jun 9, 2014

@alexcrichton So this failed on the 32-bit Linux bot, which I've fixed, and it looks like it's bootstrapped on the other bots before the builds were interrupted so hopefully it should go through next time.

@ghost
Copy link
Author

ghost commented Jun 9, 2014

@alexcrichton Oh dear, it's Android now. Third time's a charm?

I also went ahead and cherry-picked @cmr's fc44834 from #14479.

@emberian
Copy link
Member

emberian commented Jun 9, 2014

The compiler, today, should reject #[repr(C)] on structs, so that's not a very good idea.

@emberian
Copy link
Member

emberian commented Jun 9, 2014

Ah I see, nevermind. First time seeing this patch.

bors added a commit that referenced this pull request Jun 10, 2014
This uncovered some dead code, most notably in middle/liveness.rs, which I think suggests there must be something fishy with that part of the code.

The #[allow(dead_code)] annotations on some of the fields I am not super happy about but as I understand, marker type may disappear at some point.
@bors bors closed this Jun 10, 2014
@bors bors merged commit 8e34f64 into rust-lang:master Jun 10, 2014
@ghost ghost deleted the dead-struct-fields branch July 2, 2014 23:42
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

Successfully merging this pull request may close these issues.

5 participants