1 use clippy_utils::diagnostics::span_lint_and_then;
2 use clippy_utils::source::snippet;
3 use clippy_utils::{contains_return, in_macro, is_lang_ctor, return_ty, visitors::find_all_ret_expressions};
4 use if_chain::if_chain;
5 use rustc_errors::Applicability;
6 use rustc_hir::intravisit::FnKind;
7 use rustc_hir::LangItem::{OptionSome, ResultOk};
8 use rustc_hir::{Body, ExprKind, FnDecl, HirId, Impl, ItemKind, Node};
9 use rustc_lint::{LateContext, LateLintPass};
11 use rustc_session::{declare_tool_lint, impl_lint_pass};
12 use rustc_span::symbol::sym;
15 declare_clippy_lint! {
17 /// Checks for private functions that only return `Ok` or `Some`.
19 /// ### Why is this bad?
20 /// It is not meaningful to wrap values when no `None` or `Err` is returned.
22 /// ### Known problems
23 /// There can be false positives if the function signature is designed to
24 /// fit some external requirement.
28 /// fn get_cool_number(a: bool, b: bool) -> Option<i32> {
41 /// fn get_cool_number(a: bool, b: bool) -> i32 {
52 pub UNNECESSARY_WRAPS,
54 "functions that only return `Ok` or `Some`"
57 pub struct UnnecessaryWraps {
58 avoid_breaking_exported_api: bool,
61 impl_lint_pass!(UnnecessaryWraps => [UNNECESSARY_WRAPS]);
63 impl UnnecessaryWraps {
64 pub fn new(avoid_breaking_exported_api: bool) -> Self {
66 avoid_breaking_exported_api,
71 impl<'tcx> LateLintPass<'tcx> for UnnecessaryWraps {
74 cx: &LateContext<'tcx>,
75 fn_kind: FnKind<'tcx>,
76 fn_decl: &FnDecl<'tcx>,
81 // Abort if public function/method or closure.
83 FnKind::ItemFn(..) | FnKind::Method(..) => {
84 if self.avoid_breaking_exported_api && cx.access_levels.is_exported(hir_id) {
88 FnKind::Closure => return,
91 // Abort if the method is implementing a trait or of it a trait method.
92 if let Some(Node::Item(item)) = cx.tcx.hir().find(cx.tcx.hir().get_parent_node(hir_id)) {
95 ItemKind::Impl(Impl { of_trait: Some(_), .. }) | ItemKind::Trait(..)
101 // Get the wrapper and inner types, if can't, abort.
102 let (return_type_label, lang_item, inner_type) = if let ty::Adt(adt_def, subst) = return_ty(cx, hir_id).kind() {
103 if cx.tcx.is_diagnostic_item(sym::option_type, adt_def.did) {
104 ("Option", OptionSome, subst.type_at(0))
105 } else if cx.tcx.is_diagnostic_item(sym::result_type, adt_def.did) {
106 ("Result", ResultOk, subst.type_at(0))
114 // Check if all return expression respect the following condition and collect them.
115 let mut suggs = Vec::new();
116 let can_sugg = find_all_ret_expressions(cx, &body.value, |ret_expr| {
118 if !in_macro(ret_expr.span);
119 // Check if a function call.
120 if let ExprKind::Call(func, [arg]) = ret_expr.kind;
121 // Check if OPTION_SOME or RESULT_OK, depending on return type.
122 if let ExprKind::Path(qpath) = &func.kind;
123 if is_lang_ctor(cx, qpath, lang_item);
124 // Make sure the function argument does not contain a return expression.
125 if !contains_return(arg);
130 if inner_type.is_unit() {
133 snippet(cx, arg.span.source_callsite(), "..").to_string()
144 if can_sugg && !suggs.is_empty() {
145 let (lint_msg, return_type_sugg_msg, return_type_sugg, body_sugg_msg) = if inner_type.is_unit() {
147 "this function's return value is unnecessary".to_string(),
148 "remove the return type...".to_string(),
149 snippet(cx, fn_decl.output.span(), "..").to_string(),
150 "...and then remove returned values",
155 "this function's return value is unnecessarily wrapped by `{}`",
158 format!("remove `{}` from the return type...", return_type_label),
159 inner_type.to_string(),
160 "...and then change returning expressions",
164 span_lint_and_then(cx, UNNECESSARY_WRAPS, span, lint_msg.as_str(), |diag| {
165 diag.span_suggestion(
166 fn_decl.output.span(),
167 return_type_sugg_msg.as_str(),
169 Applicability::MaybeIncorrect,
171 diag.multipart_suggestion(body_sugg_msg, suggs, Applicability::MaybeIncorrect);