Skip to content

Commit 2d8211a

Browse files
committed
Add large-error-ignored config-knob
1 parent c48592e commit 2d8211a

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
@@ -7121,6 +7121,7 @@ Released 2018-09-13
71217121
[`future-size-threshold`]: https://doc.rust-lang.org/clippy/lint_configuration.html#future-size-threshold
71227122
[`ignore-interior-mutability`]: https://doc.rust-lang.org/clippy/lint_configuration.html#ignore-interior-mutability
71237123
[`inherent-impl-lint-scope`]: https://doc.rust-lang.org/clippy/lint_configuration.html#inherent-impl-lint-scope
7124+
[`large-error-ignored`]: https://doc.rust-lang.org/clippy/lint_configuration.html#large-error-ignored
71247125
[`large-error-threshold`]: https://doc.rust-lang.org/clippy/lint_configuration.html#large-error-threshold
71257126
[`lint-commented-code`]: https://doc.rust-lang.org/clippy/lint_configuration.html#lint-commented-code
71267127
[`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
@@ -681,6 +681,17 @@ Sets the scope ("crate", "file", or "module") in which duplicate inherent `impl`
681681
* [`multiple_inherent_impl`](https://rust-lang.github.io/rust-clippy/master/index.html#multiple_inherent_impl)
682682

683683

684+
## `large-error-ignored`
685+
A list of paths to types that should be ignored as overly large `Err`-variants in a
686+
`Result` returned from a function
687+
688+
**Default Value:** `[]`
689+
690+
---
691+
**Affected lints:**
692+
* [`result_large_err`](https://rust-lang.github.io/rust-clippy/master/index.html#result_large_err)
693+
694+
684695
## `large-error-threshold`
685696
The maximum size of the `Err`-variant in a `Result` returned from a function
686697

clippy_config/src/conf.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,10 @@ define_Conf! {
666666
/// Sets the scope ("crate", "file", or "module") in which duplicate inherent `impl` blocks for the same type are linted.
667667
#[lints(multiple_inherent_impl)]
668668
inherent_impl_lint_scope: InherentImplLintScope = InherentImplLintScope::Crate,
669+
/// A list of paths to types that should be ignored as overly large `Err`-variants in a
670+
/// `Result` returned from a function
671+
#[lints(result_large_err)]
672+
large_error_ignored: Vec<String> = Vec::default(),
669673
/// The maximum size of the `Err`-variant in a `Result` returned from a function
670674
#[lints(result_large_err)]
671675
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
@@ -4,6 +4,7 @@ use rustc_errors::Diag;
44
use rustc_hir as hir;
55
use rustc_lint::{LateContext, LintContext};
66
use rustc_middle::ty::{self, Ty};
7+
use rustc_span::def_id::DefIdSet;
78
use rustc_span::{Span, sym};
89

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

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

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

6977
pub(super) fn check_trait_item<'tcx>(
7078
cx: &LateContext<'tcx>,
7179
item: &hir::TraitItem<'tcx>,
7280
large_err_threshold: u64,
81+
large_err_ignored: &DefIdSet,
7382
msrv: Msrv,
7483
) {
7584
if let hir::TraitItemKind::Fn(ref sig, _) = item.kind {
@@ -78,7 +87,7 @@ pub(super) fn check_trait_item<'tcx>(
7887
if cx.effective_visibilities.is_exported(item.owner_id.def_id) {
7988
check_result_unit_err(cx, err_ty, fn_header_span, msrv);
8089
}
81-
check_result_large_err(cx, err_ty, hir_ty.span, large_err_threshold);
90+
check_result_large_err(cx, err_ty, hir_ty.span, large_err_threshold, large_err_ignored);
8291
}
8392
}
8493
}
@@ -96,7 +105,18 @@ fn check_result_unit_err(cx: &LateContext<'_>, err_ty: Ty<'_>, fn_header_span: S
96105
}
97106
}
98107

99-
fn check_result_large_err<'tcx>(cx: &LateContext<'tcx>, err_ty: Ty<'tcx>, hir_ty_span: Span, large_err_threshold: u64) {
108+
fn check_result_large_err<'tcx>(
109+
cx: &LateContext<'tcx>,
110+
err_ty: Ty<'tcx>,
111+
hir_ty_span: Span,
112+
large_err_threshold: u64,
113+
large_err_ignored: &DefIdSet,
114+
) {
115+
if let ty::Adt(adt, _) = err_ty.kind()
116+
&& large_err_ignored.contains(&adt.did())
117+
{
118+
return;
119+
}
100120
if let ty::Adt(adt, subst) = err_ty.kind()
101121
&& let Some(local_def_id) = adt.did().as_local()
102122
&& 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
@@ -50,6 +50,7 @@ error: error reading Clippy's configuration file: unknown field `foobar`, expect
5050
future-size-threshold
5151
ignore-interior-mutability
5252
inherent-impl-lint-scope
53+
large-error-ignored
5354
large-error-threshold
5455
lint-commented-code
5556
literal-representation-threshold
@@ -147,6 +148,7 @@ error: error reading Clippy's configuration file: unknown field `barfoo`, expect
147148
future-size-threshold
148149
ignore-interior-mutability
149150
inherent-impl-lint-scope
151+
large-error-ignored
150152
large-error-threshold
151153
lint-commented-code
152154
literal-representation-threshold
@@ -244,6 +246,7 @@ error: error reading Clippy's configuration file: unknown field `allow_mixed_uni
244246
future-size-threshold
245247
ignore-interior-mutability
246248
inherent-impl-lint-scope
249+
large-error-ignored
247250
large-error-threshold
248251
lint-commented-code
249252
literal-representation-threshold

0 commit comments

Comments
 (0)