summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/clippy_lints/src/question_mark_used.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:50 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:50 +0000
commit2e00214b3efbdfeefaa0fe9e8b8fd519de7adc35 (patch)
treed325add32978dbdc1db975a438b3a77d571b1ab8 /src/tools/clippy/clippy_lints/src/question_mark_used.rs
parentReleasing progress-linux version 1.68.2+dfsg1-1~progress7.99u1. (diff)
downloadrustc-2e00214b3efbdfeefaa0fe9e8b8fd519de7adc35.tar.xz
rustc-2e00214b3efbdfeefaa0fe9e8b8fd519de7adc35.zip
Merging upstream version 1.69.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/tools/clippy/clippy_lints/src/question_mark_used.rs')
-rw-r--r--src/tools/clippy/clippy_lints/src/question_mark_used.rs52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/tools/clippy/clippy_lints/src/question_mark_used.rs b/src/tools/clippy/clippy_lints/src/question_mark_used.rs
new file mode 100644
index 000000000..9b678e8d7
--- /dev/null
+++ b/src/tools/clippy/clippy_lints/src/question_mark_used.rs
@@ -0,0 +1,52 @@
+use clippy_utils::diagnostics::span_lint_and_help;
+
+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_tool_lint};
+
+declare_clippy_lint! {
+ /// ### What it does
+ /// Checks for expressions that use the question mark operator and rejects them.
+ ///
+ /// ### Why is this bad?
+ /// 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);
+ /// ```
+ #[clippy::version = "pre 1.29.0"]
+ 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<'_>) {
+ if let ExprKind::Match(_, _, MatchSource::TryDesugar) = expr.kind {
+ if !span_is_local(expr.span) {
+ return;
+ }
+
+ span_lint_and_help(
+ cx,
+ QUESTION_MARK_USED,
+ expr.span,
+ "question mark operator was used",
+ None,
+ "consider using a custom macro or match expression",
+ );
+ }
+ }
+}