-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
error on unused struct fields? #12854
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
Comments
This won't work if the layout/contents of a struct are not under the control of the programmer. For example when implementing a device driver you might need a struct like: const SomeDeviceThing = extern struct {
flags: u8,
id: u8,
enabled: u8,
}; If you only use So some discard syntax will have to be provided that is analogous to |
@leecanon I was also thinking of that use case. What about non extern structs? |
all fields are public so this feels like a non-starter. a field might not be being used by me, but it might be needed for someone else |
I thought about "my own" structs, not extern.
No doubt! But fields unused "by me" may be used outside of "my code" and if there is a field which is "globally" unused then it is not needed. |
"my code" here refers to my entire program, not my library. if me and you are writing our own completely separate programs and you use all the fields of libFoo but I don't, I don't want program to not compile simply because I don't touch the entire api surface |
but field unused by you is for sure used in the libFoo itself... |
If a struct is imported from "my project directory" it is definitely "my struct" and detecting unused fields won't hurt nobody. |
this issue is not about "changing the language" |
Would work as long as the structs themselves are private and not This is then similar to unused private functions. |
I would extend this up to "anything local to a project", not just a file. |
Alternate idea When a non-packed, non-extern struct field has no usages with side effects (including no I imagine this would be particularly good for libraries. Features not in use would just not consume memory. |
I would love to do this, but it is not possible since whether a field is used can depend on semantic analysis, which depends on conditional compilation, which means the compiler does not know whether a field is truly unused. Related: |
ok, a man is the king in his house. so rubble detection should be implemented |
Currently Zig allows structs to have fields which are never used in a program, for example:
It would be nice if Zig could detect this and generate an error similar to the error about unused function arguments.
P.S. I always wanted to have such a feature in C.
The text was updated successfully, but these errors were encountered: