|
1 | 1 | mod must_use; |
2 | 2 | mod not_unsafe_ptr_arg_deref; |
| 3 | +mod result_unit_err; |
3 | 4 | mod too_many_arguments; |
4 | 5 | mod too_many_lines; |
5 | 6 |
|
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; |
10 | 7 | use rustc_hir as hir; |
11 | 8 | 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}; |
15 | 10 | 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; |
18 | 12 |
|
19 | 13 | declare_clippy_lint! { |
20 | 14 | /// **What it does:** Checks for functions with too many parameters. |
@@ -250,65 +244,24 @@ impl<'tcx> LateLintPass<'tcx> for Functions { |
250 | 244 | hir_id: hir::HirId, |
251 | 245 | ) { |
252 | 246 | 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); |
254 | 248 | not_unsafe_ptr_arg_deref::check_fn(cx, kind, decl, body, hir_id); |
255 | 249 | } |
256 | 250 |
|
257 | 251 | fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) { |
258 | 252 | 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); |
266 | 254 | } |
267 | 255 |
|
268 | 256 | fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::ImplItem<'_>) { |
269 | 257 | 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); |
277 | 259 | } |
278 | 260 |
|
279 | 261 | fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::TraitItem<'_>) { |
280 | 262 | too_many_arguments::check_trait_item(cx, item, self.too_many_arguments_threshold); |
281 | 263 | not_unsafe_ptr_arg_deref::check_trait_item(cx, item); |
282 | 264 | 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); |
313 | 266 | } |
314 | 267 | } |
0 commit comments