2024-08-08 12:13:50 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_then;
|
2023-02-25 18:08:29 -06:00
|
|
|
|
|
|
|
use clippy_utils::macros::span_is_local;
|
|
|
|
use rustc_hir::{Expr, ExprKind, MatchSource};
|
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2023-12-01 11:21:58 -06:00
|
|
|
use rustc_session::declare_lint_pass;
|
2023-02-25 18:08:29 -06:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
|
|
|
/// ### What it does
|
|
|
|
/// Checks for expressions that use the question mark operator and rejects them.
|
|
|
|
///
|
2024-05-30 03:49:05 -05:00
|
|
|
/// ### Why restrict this?
|
2023-02-25 18:08:29 -06:00
|
|
|
/// Sometimes code wants to avoid the question mark 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);
|
|
|
|
/// ```
|
2023-04-23 06:03:09 -05:00
|
|
|
#[clippy::version = "1.69.0"]
|
2023-02-25 18:08:29 -06:00
|
|
|
pub QUESTION_MARK_USED,
|
|
|
|
restriction,
|
|
|
|
"complains if the question mark 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<'_>) {
|
2023-08-14 14:25:01 -05:00
|
|
|
if let ExprKind::Match(_, _, MatchSource::TryDesugar(_)) = expr.kind {
|
2023-02-25 18:08:29 -06:00
|
|
|
if !span_is_local(expr.span) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-08-08 12:13:50 -05:00
|
|
|
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
|
|
|
|
span_lint_and_then(
|
2023-02-25 18:08:29 -06:00
|
|
|
cx,
|
|
|
|
QUESTION_MARK_USED,
|
|
|
|
expr.span,
|
|
|
|
"question mark operator was used",
|
2024-08-08 12:13:50 -05:00
|
|
|
|diag| {
|
|
|
|
diag.help("consider using a custom macro or match expression");
|
|
|
|
},
|
2023-02-25 18:08:29 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|