diff options
| author | bors <bors@rust-lang.org> | 2022-08-30 18:20:45 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2022-08-30 18:20:45 +0000 |
| commit | 09e4659a862f8a244fedf473a06b9a85448d3a17 (patch) | |
| tree | 7829e93161de14c2254bc1f18d06c994ed8b0ea7 /tests | |
| parent | 4df60321003440948a6628c348746e94a1518ae0 (diff) | |
| parent | 66a97055b2721700b472e4460a560c8ded70b3c2 (diff) | |
| download | rust-09e4659a862f8a244fedf473a06b9a85448d3a17.tar.gz rust-09e4659a862f8a244fedf473a06b9a85448d3a17.zip | |
Auto merge of #9373 - lukaslueg:result_large_err, r=Alexendoo
Initial implementation `result_large_err`
This is a shot at #6560, #4652, and #3884. The lint checks for `Result` being returned from functions/methods where the `Err` variant is larger than a configurable threshold (the default of which is 128 bytes). There has been some discussion around this, which I'll try to quickly summarize:
* A large `Err`-variant may force an equally large `Result` if `Err` is actually bigger than `Ok`.
* There is a cost involved in large `Result`, as LLVM may choose to `memcpy` them around above a certain size.
* We usually expect the `Err` variant to be seldomly used, but pay the cost every time.
* `Result` returned from library code has a high chance of bubbling up the call stack, getting stuffed into `MyLibError { IoError(std::io::Error), ParseError(parselib::Error), ...}`, exacerbating the problem.
This PR deliberately does not take into account comparing the `Ok` to the `Err` variant (e.g. a ratio, or one being larger than the other). Rather we choose an absolute threshold for `Err`'s size, above which we warn. The reason for this is that `Err`s probably get `map_err`'ed further up the call stack, and we can't draw conclusions from the ratio at the point where the `Result` is returned. A relative threshold would also be less predictable, while not accounting for the cost of LLVM being forced to generate less efficient code if the `Err`-variant is _large_ in absolute terms.
We lint private functions as well as public functions, as the perf-cost applies to in-crate code as well.
In order to account for type-parameters, I conjured up `fn approx_ty_size`. The function relies on `LateContext::layout_of` to compute the actual size, and in case of failure (e.g. due to generics) tries to come up with an "at least size". In the latter case, the size of obviously wrong, but the inspected size certainly can't be smaller than that. Please give the approach a heavy dose of review, as I'm not actually familiar with the type-system at all (read: I have no idea what I'm doing).
The approach does, however flimsy it is, allow us to successfully lint situations like
```rust
pub union UnionError<T: Copy> {
_maybe: T,
_or_perhaps_even: (T, [u8; 512]),
}
// We know `UnionError<T>` will be at least 512 bytes, no matter what `T` is
pub fn param_large_union<T: Copy>() -> Result<(), UnionError<T>> {
Ok(())
}
```
I've given some refactoring to `functions/result_unit_err.rs` to re-use some bits. This is also the groundwork for #6409
The default threshold is 128 because of https://github.com/rust-lang/rust-clippy/issues/4652#issue-505670554
`lintcheck` does not trigger this lint for a threshold of 128. It does warn for 64, though.
The suggestion currently is the following, which is just a placeholder for discussion to be had. I did have the computed size in a `span_label`. However, that might cause both ui-tests here and lints elsewhere to become flaky wrt to their output (as the size is platform dependent).
```
error: the `Err`-variant returned via this `Result` is very large
--> $DIR/result_large_err.rs:36:34
|
LL | pub fn param_large_error<R>() -> Result<(), (u128, R, FullyDefinedLargeError)> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The `Err` variant is unusually large, at least 128 bytes
```
changelog: Add [`result_large_err`] lint
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr | 1 | ||||
| -rw-r--r-- | tests/ui/result_large_err.rs | 98 | ||||
| -rw-r--r-- | tests/ui/result_large_err.stderr | 91 |
3 files changed, 190 insertions, 0 deletions
diff --git a/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr b/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr index 9f8e778b3b9..a52a0b5289f 100644 --- a/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr +++ b/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr @@ -19,6 +19,7 @@ error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown fie enforced-import-renames enum-variant-name-threshold enum-variant-size-threshold + large-error-threshold literal-representation-threshold max-fn-params-bools max-include-file-size diff --git a/tests/ui/result_large_err.rs b/tests/ui/result_large_err.rs new file mode 100644 index 00000000000..78d8f76fe66 --- /dev/null +++ b/tests/ui/result_large_err.rs @@ -0,0 +1,98 @@ +#![warn(clippy::result_large_err)] + +pub fn small_err() -> Result<(), u128> { + Ok(()) +} + +pub fn large_err() -> Result<(), [u8; 512]> { + Ok(()) +} + +pub struct FullyDefinedLargeError { + _foo: u128, + _bar: [u8; 100], + _foobar: [u8; 120], +} + +impl FullyDefinedLargeError { + pub fn ret() -> Result<(), Self> { + Ok(()) + } +} + +pub fn struct_error() -> Result<(), FullyDefinedLargeError> { + Ok(()) +} + +type Fdlr<T> = std::result::Result<T, FullyDefinedLargeError>; +pub fn large_err_via_type_alias<T>(x: T) -> Fdlr<T> { + Ok(x) +} + +pub fn param_small_error<R>() -> Result<(), (R, u128)> { + Ok(()) +} + +pub fn param_large_error<R>() -> Result<(), (u128, R, FullyDefinedLargeError)> { + Ok(()) +} + +pub enum LargeErrorVariants<T> { + _Small(u8), + _Omg([u8; 512]), + _Param(T), +} + +impl LargeErrorVariants<()> { + pub fn large_enum_error() -> Result<(), Self> { + Ok(()) + } +} + +trait TraitForcesLargeError { + fn large_error() -> Result<(), [u8; 512]> { + Ok(()) + } +} + +struct TraitImpl; + +impl TraitForcesLargeError for TraitImpl { + // Should not lint + fn large_error() -> Result<(), [u8; 512]> { + Ok(()) + } +} + +pub union FullyDefinedUnionError { + _maybe: u8, + _or_even: [[u8; 16]; 32], +} + +pub fn large_union_err() -> Result<(), FullyDefinedUnionError> { + Ok(()) +} + +pub union UnionError<T: Copy> { + _maybe: T, + _or_perhaps_even: (T, [u8; 512]), +} + +pub fn param_large_union<T: Copy>() -> Result<(), UnionError<T>> { + Ok(()) +} + +pub struct ArrayError<T, U> { + _large_array: [T; 32], + _other_stuff: U, +} + +pub fn array_error_subst<U>() -> Result<(), ArrayError<i32, U>> { + Ok(()) +} + +pub fn array_error<T, U>() -> Result<(), ArrayError<(i32, T), U>> { + Ok(()) +} + +fn main() {} diff --git a/tests/ui/result_large_err.stderr b/tests/ui/result_large_err.stderr new file mode 100644 index 00000000000..0f1f39d72cb --- /dev/null +++ b/tests/ui/result_large_err.stderr @@ -0,0 +1,91 @@ +error: the `Err`-variant returned from this function is very large + --> $DIR/result_large_err.rs:7:23 + | +LL | pub fn large_err() -> Result<(), [u8; 512]> { + | ^^^^^^^^^^^^^^^^^^^^^ the `Err`-variant is at least 512 bytes + | + = note: `-D clippy::result-large-err` implied by `-D warnings` + = help: try reducing the size of `[u8; 512]`, for example by boxing large elements or replacing it with `Box<[u8; 512]>` + +error: the `Err`-variant returned from this function is very large + --> $DIR/result_large_err.rs:18:21 + | +LL | pub fn ret() -> Result<(), Self> { + | ^^^^^^^^^^^^^^^^ the `Err`-variant is at least 240 bytes + | + = help: try reducing the size of `FullyDefinedLargeError`, for example by boxing large elements or replacing it with `Box<FullyDefinedLargeError>` + +error: the `Err`-variant returned from this function is very large + --> $DIR/result_large_err.rs:23:26 + | +LL | pub fn struct_error() -> Result<(), FullyDefinedLargeError> { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the `Err`-variant is at least 240 bytes + | + = help: try reducing the size of `FullyDefinedLargeError`, for example by boxing large elements or replacing it with `Box<FullyDefinedLargeError>` + +error: the `Err`-variant returned from this function is very large + --> $DIR/result_large_err.rs:28:45 + | +LL | pub fn large_err_via_type_alias<T>(x: T) -> Fdlr<T> { + | ^^^^^^^ the `Err`-variant is at least 240 bytes + | + = help: try reducing the size of `FullyDefinedLargeError`, for example by boxing large elements or replacing it with `Box<FullyDefinedLargeError>` + +error: the `Err`-variant returned from this function is very large + --> $DIR/result_large_err.rs:36:34 + | +LL | pub fn param_large_error<R>() -> Result<(), (u128, R, FullyDefinedLargeError)> { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the `Err`-variant is at least 256 bytes + | + = help: try reducing the size of `(u128, R, FullyDefinedLargeError)`, for example by boxing large elements or replacing it with `Box<(u128, R, FullyDefinedLargeError)>` + +error: the `Err`-variant returned from this function is very large + --> $DIR/result_large_err.rs:47:34 + | +LL | pub fn large_enum_error() -> Result<(), Self> { + | ^^^^^^^^^^^^^^^^ the `Err`-variant is at least 513 bytes + | + = help: try reducing the size of `LargeErrorVariants<()>`, for example by boxing large elements or replacing it with `Box<LargeErrorVariants<()>>` + +error: the `Err`-variant returned from this function is very large + --> $DIR/result_large_err.rs:53:25 + | +LL | fn large_error() -> Result<(), [u8; 512]> { + | ^^^^^^^^^^^^^^^^^^^^^ the `Err`-variant is at least 512 bytes + | + = help: try reducing the size of `[u8; 512]`, for example by boxing large elements or replacing it with `Box<[u8; 512]>` + +error: the `Err`-variant returned from this function is very large + --> $DIR/result_large_err.rs:72:29 + | +LL | pub fn large_union_err() -> Result<(), FullyDefinedUnionError> { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the `Err`-variant is at least 512 bytes + | + = help: try reducing the size of `FullyDefinedUnionError`, for example by boxing large elements or replacing it with `Box<FullyDefinedUnionError>` + +error: the `Err`-variant returned from this function is very large + --> $DIR/result_large_err.rs:81:40 + | +LL | pub fn param_large_union<T: Copy>() -> Result<(), UnionError<T>> { + | ^^^^^^^^^^^^^^^^^^^^^^^^^ the `Err`-variant is at least 512 bytes + | + = help: try reducing the size of `UnionError<T>`, for example by boxing large elements or replacing it with `Box<UnionError<T>>` + +error: the `Err`-variant returned from this function is very large + --> $DIR/result_large_err.rs:90:34 + | +LL | pub fn array_error_subst<U>() -> Result<(), ArrayError<i32, U>> { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the `Err`-variant is at least 128 bytes + | + = help: try reducing the size of `ArrayError<i32, U>`, for example by boxing large elements or replacing it with `Box<ArrayError<i32, U>>` + +error: the `Err`-variant returned from this function is very large + --> $DIR/result_large_err.rs:94:31 + | +LL | pub fn array_error<T, U>() -> Result<(), ArrayError<(i32, T), U>> { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the `Err`-variant is at least 128 bytes + | + = help: try reducing the size of `ArrayError<(i32, T), U>`, for example by boxing large elements or replacing it with `Box<ArrayError<(i32, T), U>>` + +error: aborting due to 11 previous errors + |
