1 use clippy_utils::diagnostics::span_lint_and_sugg;
2 use clippy_utils::source::position_before_rarrow;
3 use if_chain::if_chain;
5 use rustc_ast::visit::FnKind;
6 use rustc_errors::Applicability;
7 use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
8 use rustc_session::{declare_lint_pass, declare_tool_lint};
9 use rustc_span::source_map::Span;
10 use rustc_span::BytePos;
12 declare_clippy_lint! {
13 /// **What it does:** Checks for unit (`()`) expressions that can be removed.
15 /// **Why is this bad?** Such expressions add no value, but can make the code
16 /// less readable. Depending on formatting they can make a `break` or `return`
17 /// statement look like a function call.
19 /// **Known problems:** None.
23 /// fn return_unit() -> () {
29 /// fn return_unit() {}
33 "needless unit expression"
36 declare_lint_pass!(UnusedUnit => [UNUSED_UNIT]);
38 impl EarlyLintPass for UnusedUnit {
39 fn check_fn(&mut self, cx: &EarlyContext<'_>, kind: FnKind<'_>, span: Span, _: ast::NodeId) {
41 if let ast::FnRetTy::Ty(ref ty) = kind.decl().output;
42 if let ast::TyKind::Tup(ref vals) = ty.kind;
43 if vals.is_empty() && !ty.span.from_expansion() && get_def(span) == get_def(ty.span);
45 lint_unneeded_unit_return(cx, ty, span);
50 fn check_block(&mut self, cx: &EarlyContext<'_>, block: &ast::Block) {
52 if let Some(stmt) = block.stmts.last();
53 if let ast::StmtKind::Expr(ref expr) = stmt.kind;
54 if is_unit_expr(expr);
55 let ctxt = block.span.ctxt();
56 if stmt.span.ctxt() == ctxt && expr.span.ctxt() == ctxt;
63 "unneeded unit expression",
64 "remove the final `()`",
66 Applicability::MachineApplicable,
72 fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) {
74 ast::ExprKind::Ret(Some(ref expr)) | ast::ExprKind::Break(_, Some(ref expr)) => {
75 if is_unit_expr(expr) && !expr.span.from_expansion() {
83 Applicability::MachineApplicable,
91 fn check_poly_trait_ref(&mut self, cx: &EarlyContext<'_>, poly: &ast::PolyTraitRef, _: &ast::TraitBoundModifier) {
92 let segments = &poly.trait_ref.path.segments;
95 if segments.len() == 1;
96 if ["Fn", "FnMut", "FnOnce"].contains(&&*segments[0].ident.name.as_str());
97 if let Some(args) = &segments[0].args;
98 if let ast::GenericArgs::Parenthesized(generic_args) = &**args;
99 if let ast::FnRetTy::Ty(ty) = &generic_args.output;
100 if ty.kind.is_unit();
102 lint_unneeded_unit_return(cx, ty, generic_args.span);
110 fn get_def(span: Span) -> Option<Span> {
111 if span.from_expansion() {
112 Some(span.ctxt().outer_expn_data().def_site)
118 // is this expr a `()` unit?
119 fn is_unit_expr(expr: &ast::Expr) -> bool {
120 if let ast::ExprKind::Tup(ref vals) = expr.kind {
127 fn lint_unneeded_unit_return(cx: &EarlyContext<'_>, ty: &ast::Ty, span: Span) {
128 let (ret_span, appl) = if let Ok(fn_source) = cx.sess().source_map().span_to_snippet(span.with_hi(ty.span.hi())) {
129 position_before_rarrow(&fn_source).map_or((ty.span, Applicability::MaybeIncorrect), |rpos| {
131 #[allow(clippy::cast_possible_truncation)]
132 ty.span.with_lo(BytePos(span.lo().0 + rpos as u32)),
133 Applicability::MachineApplicable,
137 (ty.span, Applicability::MaybeIncorrect)
143 "unneeded unit return type",
144 "remove the `-> ()`",