-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathquestion_mark_used.rs
48 lines (43 loc) · 1.51 KB
/
question_mark_used.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::macros::span_is_local;
use rustc_hir::{Expr, ExprKind, MatchSource};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::declare_lint_pass;
declare_clippy_lint! {
/// ### What it does
/// Checks for expressions that use the `?` operator and rejects them.
///
/// ### Why restrict this?
/// Sometimes code wants to avoid the `?` operator because for instance a local
/// block requires a macro to re-throw errors to attach additional information to the
/// error.
///
/// ### Example
/// ```ignore
/// let result = expr?;
/// ```
///
/// Could be written:
///
/// ```ignore
/// utility_macro!(expr);
/// ```
#[clippy::version = "1.69.0"]
pub QUESTION_MARK_USED,
restriction,
"checks if the `?` operator is used"
}
declare_lint_pass!(QuestionMarkUsed => [QUESTION_MARK_USED]);
impl<'tcx> LateLintPass<'tcx> for QuestionMarkUsed {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
if let ExprKind::Match(_, _, MatchSource::TryDesugar(_)) = expr.kind {
if !span_is_local(expr.span) {
return;
}
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
span_lint_and_then(cx, QUESTION_MARK_USED, expr.span, "the `?` operator was used", |diag| {
diag.help("consider using a custom macro or match expression");
});
}
}
}