-
Notifications
You must be signed in to change notification settings - Fork 1.7k
new large_enum_variants lint #1187
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
Conversation
Added linter to detect large variants and suggesting to box them
@@ -162,6 +162,8 @@ define_Conf! { | |||
("too-large-for-stack", too_large_for_stack, 200 => u64), | |||
/// Lint: ENUM_VARIANT_NAMES. The minimum number of enum variants for the lints about variant names to trigger | |||
("enum-variant-name-threshold", enum_variant_name_threshold, 3 => u64), | |||
/// Lint: ENUM_LARGE_VARIANT. The maximum size of a emum's variant to avoid box suggestion | |||
("enum-variant-size-threshold", enum_variant_size_threshold, 200 => u64), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Used the same value as 'too-large-for-stack'. I don't know which value use as default
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's already quite big for an enum variant IMO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So is the default value ok? Should I leave it to 200?
r+ modulo the default value of |
/// } | ||
/// ``` | ||
declare_lint! { | ||
pub ENUM_LARGE_VARIANT, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be named LARGE_ENUM_VARIANT
IMO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed all references to 'enum_large_variant' to 'large_enum_variant' on 088bb8f
needs a rebase + some research into enum variant sizes in existing code bases for choosing the default value. |
While I was doing some research on popular Rust projects with this lint, I discovered that under some conditions, this lint will panic. Consider the following code:
With that code, one of the calls to rustc is panicking with:
Is obvious that the compiler is not able to know the size of 'SomeTrait::Item' because it's generic and it depends on which concrete trait implementations is given. In this scenario, the lint should ignore generic enums depending on associated types? Or the lint should search for any concretion of the enum (I don't know the technical name of this, I mean: LargeEnumGeneric), and then calculate the variant sizes of this? |
@gnieto Good catch! IMO it's ok to bail (cleanly, not ICEing 😄) when the size is unknown. You could manage to look at the enumeration instances, but it might be overkill. Although, while testing this I noticed it looks like compiletest-rs does not catch ICE if all expected error are found before the ICE. This is bad, cc @Manishearth. |
hmm, weird. IIRC compiletest does check the exit code. |
@Manishearth compiletest-rs, rustc The actual error is
It looks like that function was not adapted to the JSON format. I'll do a PR tomorrow. As for the error code, it is checked but rustc returns 101 for any kind of error (so it should work in run-pass, but not compile-fail). |
It has been a while since I opened the PR , and I tried a couple of ways to avoid the internal compiler error:
If anyone has some idea on how I can approach the issue, I'll try to do it, but at the moment, I don't know how to continue. |
I think that's the right approach for now. You can always use a |
@gnieto: are you planning to continue with this PR? |
@oli-obk I don't think that I will not have time to dedicate to this in the following weeks. |
I just wanted to ask, to make sure I don't steal your PR that you still planned to continue. I'll finish it. |
Added lint to detect large variants and suggesting to box them
Solves https://github.com/Manishearth/rust-clippy/issues/1127