Skip to content

Commit 9e4d650

Browse files
committed
Add large-error-ignored config-knob
1 parent 49ae1d4 commit 9e4d650

File tree

9 files changed

+93
-9
lines changed

9 files changed

+93
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6871,6 +6871,7 @@ Released 2018-09-13
68716871
[`excessive-nesting-threshold`]: https://doc.rust-lang.org/clippy/lint_configuration.html#excessive-nesting-threshold
68726872
[`future-size-threshold`]: https://doc.rust-lang.org/clippy/lint_configuration.html#future-size-threshold
68736873
[`ignore-interior-mutability`]: https://doc.rust-lang.org/clippy/lint_configuration.html#ignore-interior-mutability
6874+
[`large-error-ignored`]: https://doc.rust-lang.org/clippy/lint_configuration.html#large-error-ignored
68746875
[`large-error-threshold`]: https://doc.rust-lang.org/clippy/lint_configuration.html#large-error-threshold
68756876
[`lint-commented-code`]: https://doc.rust-lang.org/clippy/lint_configuration.html#lint-commented-code
68766877
[`literal-representation-threshold`]: https://doc.rust-lang.org/clippy/lint_configuration.html#literal-representation-threshold

book/src/lint_configuration.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,17 @@ A list of paths to types that should be treated as if they do not contain interi
671671
* [`mutable_key_type`](https://rust-lang.github.io/rust-clippy/master/index.html#mutable_key_type)
672672

673673

674+
## `large-error-ignored`
675+
A list of paths to types that should be ignored as overly large `Err`-variants in a
676+
`Result` returned from a function
677+
678+
**Default Value:** `[]`
679+
680+
---
681+
**Affected lints:**
682+
* [`result_large_err`](https://rust-lang.github.io/rust-clippy/master/index.html#result_large_err)
683+
684+
674685
## `large-error-threshold`
675686
The maximum size of the `Err`-variant in a `Result` returned from a function
676687

clippy_config/src/conf.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,10 @@ define_Conf! {
663663
/// A list of paths to types that should be treated as if they do not contain interior mutability
664664
#[lints(borrow_interior_mutable_const, declare_interior_mutable_const, ifs_same_cond, mutable_key_type)]
665665
ignore_interior_mutability: Vec<String> = Vec::from(["bytes::Bytes".into()]),
666+
/// A list of paths to types that should be ignored as overly large `Err`-variants in a
667+
/// `Result` returned from a function
668+
#[lints(result_large_err)]
669+
large_error_ignored: Vec<String> = Vec::default(),
666670
/// The maximum size of the `Err`-variant in a `Result` returned from a function
667671
#[lints(result_large_err)]
668672
large_error_threshold: u64 = 128,

clippy_lints/src/functions/mod.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,7 @@ pub struct Functions {
485485
too_many_arguments_threshold: u64,
486486
too_many_lines_threshold: u64,
487487
large_error_threshold: u64,
488+
large_error_ignored: DefIdSet,
488489
avoid_breaking_exported_api: bool,
489490
/// A set of resolved `def_id` of traits that are configured to allow
490491
/// function params renaming.
@@ -498,6 +499,11 @@ impl Functions {
498499
too_many_arguments_threshold: conf.too_many_arguments_threshold,
499500
too_many_lines_threshold: conf.too_many_lines_threshold,
500501
large_error_threshold: conf.large_error_threshold,
502+
large_error_ignored: conf
503+
.large_error_ignored
504+
.iter()
505+
.flat_map(|ignored_ty| lookup_path_str(tcx, PathNS::Type, ignored_ty))
506+
.collect(),
501507
avoid_breaking_exported_api: conf.avoid_breaking_exported_api,
502508
trait_ids: conf
503509
.allow_renamed_params_for
@@ -554,12 +560,24 @@ impl<'tcx> LateLintPass<'tcx> for Functions {
554560

555561
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) {
556562
must_use::check_item(cx, item);
557-
result::check_item(cx, item, self.large_error_threshold, self.msrv);
563+
result::check_item(
564+
cx,
565+
item,
566+
self.large_error_threshold,
567+
&self.large_error_ignored,
568+
self.msrv,
569+
);
558570
}
559571

560572
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::ImplItem<'_>) {
561573
must_use::check_impl_item(cx, item);
562-
result::check_impl_item(cx, item, self.large_error_threshold, self.msrv);
574+
result::check_impl_item(
575+
cx,
576+
item,
577+
self.large_error_threshold,
578+
&self.large_error_ignored,
579+
self.msrv,
580+
);
563581
impl_trait_in_params::check_impl_item(cx, item);
564582
renamed_function_params::check_impl_item(cx, item, &self.trait_ids);
565583
}
@@ -568,7 +586,13 @@ impl<'tcx> LateLintPass<'tcx> for Functions {
568586
too_many_arguments::check_trait_item(cx, item, self.too_many_arguments_threshold);
569587
not_unsafe_ptr_arg_deref::check_trait_item(cx, item);
570588
must_use::check_trait_item(cx, item);
571-
result::check_trait_item(cx, item, self.large_error_threshold, self.msrv);
589+
result::check_trait_item(
590+
cx,
591+
item,
592+
self.large_error_threshold,
593+
&self.large_error_ignored,
594+
self.msrv,
595+
);
572596
impl_trait_in_params::check_trait_item(cx, item, self.avoid_breaking_exported_api);
573597
ref_option::check_trait_item(cx, item, self.avoid_breaking_exported_api);
574598
}

clippy_lints/src/functions/result.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use rustc_errors::Diag;
33
use rustc_hir as hir;
44
use rustc_lint::{LateContext, LintContext};
55
use rustc_middle::ty::{self, Ty};
6+
use rustc_span::def_id::DefIdSet;
67
use rustc_span::{Span, sym};
78

89
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_then};
@@ -34,22 +35,29 @@ fn result_err_ty<'tcx>(
3435
}
3536
}
3637

37-
pub(super) fn check_item<'tcx>(cx: &LateContext<'tcx>, item: &hir::Item<'tcx>, large_err_threshold: u64, msrv: Msrv) {
38+
pub(super) fn check_item<'tcx>(
39+
cx: &LateContext<'tcx>,
40+
item: &hir::Item<'tcx>,
41+
large_err_threshold: u64,
42+
large_err_ignored: &DefIdSet,
43+
msrv: Msrv,
44+
) {
3845
if let hir::ItemKind::Fn { ref sig, .. } = item.kind
3946
&& let Some((hir_ty, err_ty)) = result_err_ty(cx, sig.decl, item.owner_id.def_id, item.span)
4047
{
4148
if cx.effective_visibilities.is_exported(item.owner_id.def_id) {
4249
let fn_header_span = item.span.with_hi(sig.decl.output.span().hi());
4350
check_result_unit_err(cx, err_ty, fn_header_span, msrv);
4451
}
45-
check_result_large_err(cx, err_ty, hir_ty.span, large_err_threshold);
52+
check_result_large_err(cx, err_ty, hir_ty.span, large_err_threshold, large_err_ignored);
4653
}
4754
}
4855

4956
pub(super) fn check_impl_item<'tcx>(
5057
cx: &LateContext<'tcx>,
5158
item: &hir::ImplItem<'tcx>,
5259
large_err_threshold: u64,
60+
large_err_ignored: &DefIdSet,
5361
msrv: Msrv,
5462
) {
5563
// Don't lint if method is a trait's implementation, we can't do anything about those
@@ -61,14 +69,15 @@ pub(super) fn check_impl_item<'tcx>(
6169
let fn_header_span = item.span.with_hi(sig.decl.output.span().hi());
6270
check_result_unit_err(cx, err_ty, fn_header_span, msrv);
6371
}
64-
check_result_large_err(cx, err_ty, hir_ty.span, large_err_threshold);
72+
check_result_large_err(cx, err_ty, hir_ty.span, large_err_threshold, large_err_ignored);
6573
}
6674
}
6775

6876
pub(super) fn check_trait_item<'tcx>(
6977
cx: &LateContext<'tcx>,
7078
item: &hir::TraitItem<'tcx>,
7179
large_err_threshold: u64,
80+
large_err_ignored: &DefIdSet,
7281
msrv: Msrv,
7382
) {
7483
if let hir::TraitItemKind::Fn(ref sig, _) = item.kind {
@@ -77,7 +86,7 @@ pub(super) fn check_trait_item<'tcx>(
7786
if cx.effective_visibilities.is_exported(item.owner_id.def_id) {
7887
check_result_unit_err(cx, err_ty, fn_header_span, msrv);
7988
}
80-
check_result_large_err(cx, err_ty, hir_ty.span, large_err_threshold);
89+
check_result_large_err(cx, err_ty, hir_ty.span, large_err_threshold, large_err_ignored);
8190
}
8291
}
8392
}
@@ -95,7 +104,18 @@ fn check_result_unit_err(cx: &LateContext<'_>, err_ty: Ty<'_>, fn_header_span: S
95104
}
96105
}
97106

98-
fn check_result_large_err<'tcx>(cx: &LateContext<'tcx>, err_ty: Ty<'tcx>, hir_ty_span: Span, large_err_threshold: u64) {
107+
fn check_result_large_err<'tcx>(
108+
cx: &LateContext<'tcx>,
109+
err_ty: Ty<'tcx>,
110+
hir_ty_span: Span,
111+
large_err_threshold: u64,
112+
large_err_ignored: &DefIdSet,
113+
) {
114+
if let ty::Adt(adt, _) = err_ty.kind()
115+
&& large_err_ignored.contains(&adt.did())
116+
{
117+
return;
118+
}
99119
if let ty::Adt(adt, subst) = err_ty.kind()
100120
&& let Some(local_def_id) = adt.did().as_local()
101121
&& let hir::Node::Item(item) = cx.tcx.hir_node_by_def_id(local_def_id)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
large-error-threshold = 512
2+
large-error-ignored = ["result_large_err::IgnoredError", "result_large_err::IgnoredErrorEnum"]

tests/ui-toml/result_large_err/result_large_err.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
//@compile-flags: --crate-name result_large_err
12
#![warn(clippy::result_large_err)]
3+
#![allow(clippy::large_enum_variant)]
24

35
fn f() -> Result<(), [u8; 511]> {
46
todo!()
@@ -7,4 +9,22 @@ fn f2() -> Result<(), [u8; 512]> {
79
//~^ ERROR: the `Err`-variant returned from this function is very large
810
todo!()
911
}
12+
13+
struct IgnoredError {
14+
inner: [u8; 512],
15+
}
16+
17+
fn f3() -> Result<(), IgnoredError> {
18+
todo!()
19+
}
20+
21+
enum IgnoredErrorEnum {
22+
V1,
23+
V2 { inner: [u8; 512] },
24+
}
25+
26+
fn f4() -> Result<(), IgnoredErrorEnum> {
27+
todo!()
28+
}
29+
1030
fn main() {}

tests/ui-toml/result_large_err/result_large_err.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: the `Err`-variant returned from this function is very large
2-
--> tests/ui-toml/result_large_err/result_large_err.rs:6:12
2+
--> tests/ui-toml/result_large_err/result_large_err.rs:8:12
33
|
44
LL | fn f2() -> Result<(), [u8; 512]> {
55
| ^^^^^^^^^^^^^^^^^^^^^ the `Err`-variant is at least 512 bytes

tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ error: error reading Clippy's configuration file: unknown field `foobar`, expect
4949
excessive-nesting-threshold
5050
future-size-threshold
5151
ignore-interior-mutability
52+
large-error-ignored
5253
large-error-threshold
5354
lint-commented-code
5455
literal-representation-threshold
@@ -144,6 +145,7 @@ error: error reading Clippy's configuration file: unknown field `barfoo`, expect
144145
excessive-nesting-threshold
145146
future-size-threshold
146147
ignore-interior-mutability
148+
large-error-ignored
147149
large-error-threshold
148150
lint-commented-code
149151
literal-representation-threshold
@@ -239,6 +241,7 @@ error: error reading Clippy's configuration file: unknown field `allow_mixed_uni
239241
excessive-nesting-threshold
240242
future-size-threshold
241243
ignore-interior-mutability
244+
large-error-ignored
242245
large-error-threshold
243246
lint-commented-code
244247
literal-representation-threshold

0 commit comments

Comments
 (0)