Skip to content

Commit add3e50

Browse files
committed
Move result_unit_err to its own module
1 parent c379165 commit add3e50

File tree

3 files changed

+74
-55
lines changed

3 files changed

+74
-55
lines changed

clippy_lints/src/functions/mod.rs

+7-54
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
mod must_use;
22
mod not_unsafe_ptr_arg_deref;
3+
mod result_unit_err;
34
mod too_many_arguments;
45
mod too_many_lines;
56

6-
use clippy_utils::diagnostics::span_lint_and_help;
7-
use clippy_utils::trait_ref_of_method;
8-
use clippy_utils::ty::is_type_diagnostic_item;
9-
use if_chain::if_chain;
107
use rustc_hir as hir;
118
use rustc_hir::intravisit;
12-
use rustc_lint::{LateContext, LateLintPass, LintContext};
13-
use rustc_middle::lint::in_external_macro;
14-
use rustc_middle::ty;
9+
use rustc_lint::{LateContext, LateLintPass};
1510
use rustc_session::{declare_tool_lint, impl_lint_pass};
16-
use rustc_span::{sym, Span};
17-
use rustc_typeck::hir_ty_to_ty;
11+
use rustc_span::Span;
1812

1913
declare_clippy_lint! {
2014
/// **What it does:** Checks for functions with too many parameters.
@@ -250,65 +244,24 @@ impl<'tcx> LateLintPass<'tcx> for Functions {
250244
hir_id: hir::HirId,
251245
) {
252246
too_many_arguments::check_fn(cx, kind, decl, span, hir_id, self.too_many_arguments_threshold);
253-
too_many_lines::check(cx, span, body, self.too_many_lines_threshold);
247+
too_many_lines::check_fn(cx, span, body, self.too_many_lines_threshold);
254248
not_unsafe_ptr_arg_deref::check_fn(cx, kind, decl, body, hir_id);
255249
}
256250

257251
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) {
258252
must_use::check_item(cx, item);
259-
if let hir::ItemKind::Fn(ref sig, ref _generics, _) = item.kind {
260-
let is_public = cx.access_levels.is_exported(item.hir_id());
261-
let fn_header_span = item.span.with_hi(sig.decl.output.span().hi());
262-
if is_public {
263-
check_result_unit_err(cx, &sig.decl, item.span, fn_header_span);
264-
}
265-
}
253+
result_unit_err::check_item(cx, item);
266254
}
267255

268256
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::ImplItem<'_>) {
269257
must_use::check_impl_item(cx, item);
270-
if let hir::ImplItemKind::Fn(ref sig, _) = item.kind {
271-
let is_public = cx.access_levels.is_exported(item.hir_id());
272-
let fn_header_span = item.span.with_hi(sig.decl.output.span().hi());
273-
if is_public && trait_ref_of_method(cx, item.hir_id()).is_none() {
274-
check_result_unit_err(cx, &sig.decl, item.span, fn_header_span);
275-
}
276-
}
258+
result_unit_err::check_impl_item(cx, item);
277259
}
278260

279261
fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::TraitItem<'_>) {
280262
too_many_arguments::check_trait_item(cx, item, self.too_many_arguments_threshold);
281263
not_unsafe_ptr_arg_deref::check_trait_item(cx, item);
282264
must_use::check_trait_item(cx, item);
283-
284-
if let hir::TraitItemKind::Fn(ref sig, _) = item.kind {
285-
let is_public = cx.access_levels.is_exported(item.hir_id());
286-
let fn_header_span = item.span.with_hi(sig.decl.output.span().hi());
287-
if is_public {
288-
check_result_unit_err(cx, &sig.decl, item.span, fn_header_span);
289-
}
290-
}
291-
}
292-
}
293-
294-
fn check_result_unit_err(cx: &LateContext<'_>, decl: &hir::FnDecl<'_>, item_span: Span, fn_header_span: Span) {
295-
if_chain! {
296-
if !in_external_macro(cx.sess(), item_span);
297-
if let hir::FnRetTy::Return(ref ty) = decl.output;
298-
let ty = hir_ty_to_ty(cx.tcx, ty);
299-
if is_type_diagnostic_item(cx, ty, sym::result_type);
300-
if let ty::Adt(_, substs) = ty.kind();
301-
let err_ty = substs.type_at(1);
302-
if err_ty.is_unit();
303-
then {
304-
span_lint_and_help(
305-
cx,
306-
RESULT_UNIT_ERR,
307-
fn_header_span,
308-
"this returns a `Result<_, ()>",
309-
None,
310-
"use a custom Error type instead",
311-
);
312-
}
265+
result_unit_err::check_trait_item(cx, item);
313266
}
314267
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
use rustc_hir as hir;
2+
use rustc_lint::{LateContext, LintContext};
3+
use rustc_middle::lint::in_external_macro;
4+
use rustc_middle::ty;
5+
use rustc_span::{sym, Span};
6+
use rustc_typeck::hir_ty_to_ty;
7+
8+
use if_chain::if_chain;
9+
10+
use clippy_utils::diagnostics::span_lint_and_help;
11+
use clippy_utils::trait_ref_of_method;
12+
use clippy_utils::ty::is_type_diagnostic_item;
13+
14+
use super::RESULT_UNIT_ERR;
15+
16+
pub(super) fn check_item(cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) {
17+
if let hir::ItemKind::Fn(ref sig, ref _generics, _) = item.kind {
18+
let is_public = cx.access_levels.is_exported(item.hir_id());
19+
let fn_header_span = item.span.with_hi(sig.decl.output.span().hi());
20+
if is_public {
21+
check_result_unit_err(cx, &sig.decl, item.span, fn_header_span);
22+
}
23+
}
24+
}
25+
26+
pub(super) fn check_impl_item(cx: &LateContext<'tcx>, item: &'tcx hir::ImplItem<'_>) {
27+
if let hir::ImplItemKind::Fn(ref sig, _) = item.kind {
28+
let is_public = cx.access_levels.is_exported(item.hir_id());
29+
let fn_header_span = item.span.with_hi(sig.decl.output.span().hi());
30+
if is_public && trait_ref_of_method(cx, item.hir_id()).is_none() {
31+
check_result_unit_err(cx, &sig.decl, item.span, fn_header_span);
32+
}
33+
}
34+
}
35+
36+
pub(super) fn check_trait_item(cx: &LateContext<'tcx>, item: &'tcx hir::TraitItem<'_>) {
37+
if let hir::TraitItemKind::Fn(ref sig, _) = item.kind {
38+
let is_public = cx.access_levels.is_exported(item.hir_id());
39+
let fn_header_span = item.span.with_hi(sig.decl.output.span().hi());
40+
if is_public {
41+
check_result_unit_err(cx, &sig.decl, item.span, fn_header_span);
42+
}
43+
}
44+
}
45+
46+
fn check_result_unit_err(cx: &LateContext<'_>, decl: &hir::FnDecl<'_>, item_span: Span, fn_header_span: Span) {
47+
if_chain! {
48+
if !in_external_macro(cx.sess(), item_span);
49+
if let hir::FnRetTy::Return(ref ty) = decl.output;
50+
let ty = hir_ty_to_ty(cx.tcx, ty);
51+
if is_type_diagnostic_item(cx, ty, sym::result_type);
52+
if let ty::Adt(_, substs) = ty.kind();
53+
let err_ty = substs.type_at(1);
54+
if err_ty.is_unit();
55+
then {
56+
span_lint_and_help(
57+
cx,
58+
RESULT_UNIT_ERR,
59+
fn_header_span,
60+
"this returns a `Result<_, ()>",
61+
None,
62+
"use a custom Error type instead",
63+
);
64+
}
65+
}
66+
}

clippy_lints/src/functions/too_many_lines.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use clippy_utils::source::snippet;
88

99
use super::TOO_MANY_LINES;
1010

11-
pub(super) fn check(cx: &LateContext<'_>, span: Span, body: &'tcx hir::Body<'_>, too_many_lines_threshold: u64) {
11+
pub(super) fn check_fn(cx: &LateContext<'_>, span: Span, body: &'tcx hir::Body<'_>, too_many_lines_threshold: u64) {
1212
if in_external_macro(cx.sess(), span) {
1313
return;
1414
}

0 commit comments

Comments
 (0)