Skip to content

Commit 1ab0802

Browse files
committed
Add question-mark-used lint
This lint complains when the question mark operator (try operator) is used. This is a restriction lint that can be useful on local scopes where a custom error handling macro is supposed to be used to augment the error based on local scope data before returning.
1 parent ac60dca commit 1ab0802

File tree

6 files changed

+85
-0
lines changed

6 files changed

+85
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4675,6 +4675,7 @@ Released 2018-09-13
46754675
[`pub_enum_variant_names`]: https://rust-lang.github.io/rust-clippy/master/index.html#pub_enum_variant_names
46764676
[`pub_use`]: https://rust-lang.github.io/rust-clippy/master/index.html#pub_use
46774677
[`question_mark`]: https://rust-lang.github.io/rust-clippy/master/index.html#question_mark
4678+
[`question_mark_used`]: https://rust-lang.github.io/rust-clippy/master/index.html#question_mark_used
46784679
[`range_minus_one`]: https://rust-lang.github.io/rust-clippy/master/index.html#range_minus_one
46794680
[`range_plus_one`]: https://rust-lang.github.io/rust-clippy/master/index.html#range_plus_one
46804681
[`range_step_by_zero`]: https://rust-lang.github.io/rust-clippy/master/index.html#range_step_by_zero

clippy_lints/src/declared_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
507507
crate::ptr_offset_with_cast::PTR_OFFSET_WITH_CAST_INFO,
508508
crate::pub_use::PUB_USE_INFO,
509509
crate::question_mark::QUESTION_MARK_INFO,
510+
crate::question_mark_used::QUESTION_MARK_USED_INFO,
510511
crate::ranges::MANUAL_RANGE_CONTAINS_INFO,
511512
crate::ranges::RANGE_MINUS_ONE_INFO,
512513
crate::ranges::RANGE_PLUS_ONE_INFO,

clippy_lints/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ mod ptr;
242242
mod ptr_offset_with_cast;
243243
mod pub_use;
244244
mod question_mark;
245+
mod question_mark_used;
245246
mod ranges;
246247
mod rc_clone_in_vec_init;
247248
mod read_zero_byte_vec;
@@ -693,6 +694,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
693694
store.register_late_pass(|_| Box::new(implicit_hasher::ImplicitHasher));
694695
store.register_late_pass(|_| Box::new(fallible_impl_from::FallibleImplFrom));
695696
store.register_late_pass(|_| Box::new(question_mark::QuestionMark));
697+
store.register_late_pass(|_| Box::new(question_mark_used::QuestionMarkUsed));
696698
store.register_early_pass(|| Box::new(suspicious_operation_groupings::SuspiciousOperationGroupings));
697699
store.register_late_pass(|_| Box::new(suspicious_trait_impl::SuspiciousImpl));
698700
store.register_late_pass(|_| Box::new(map_unit_fn::MapUnit));
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use clippy_utils::diagnostics::span_lint_and_help;
2+
3+
use if_chain::if_chain;
4+
use rustc_hir::{Expr, ExprKind, MatchSource};
5+
use rustc_lint::{LateContext, LateLintPass};
6+
use rustc_session::{declare_lint_pass, declare_tool_lint};
7+
8+
declare_clippy_lint! {
9+
/// ### What it does
10+
/// Checks for expressions that use the question mark operator and rejects them.
11+
///
12+
/// ### Why is this bad?
13+
/// Sometimes code wants to avoid the question mark operator because for instance a local
14+
/// block requires a macro to re-throw errors to attach additional information to the
15+
/// error.
16+
///
17+
/// ### Example
18+
/// ```ignore
19+
/// let result = expr?;
20+
/// ```
21+
///
22+
/// Could be written:
23+
///
24+
/// ```ignore
25+
/// utility_macro!(expr);
26+
/// ```
27+
#[clippy::version = "pre 1.29.0"]
28+
pub QUESTION_MARK_USED,
29+
restriction,
30+
"complains if the question mark operator is used"
31+
}
32+
33+
declare_lint_pass!(QuestionMarkUsed => [QUESTION_MARK_USED]);
34+
35+
fn check(cx: &LateContext<'_>, expr: &Expr<'_>) {
36+
if_chain! {
37+
if let ExprKind::Match(_, _, MatchSource::TryDesugar) = expr.kind;
38+
then {
39+
span_lint_and_help(
40+
cx,
41+
QUESTION_MARK_USED,
42+
expr.span,
43+
"question mark operator was used",
44+
None,
45+
"consider using a custom macro or match expression"
46+
);
47+
}
48+
}
49+
}
50+
51+
impl<'tcx> LateLintPass<'tcx> for QuestionMarkUsed {
52+
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
53+
check(cx, expr);
54+
}
55+
}

tests/ui/question_mark_used.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// non rustfixable
2+
#![allow(unreachable_code)]
3+
#![allow(dead_code)]
4+
#![warn(clippy::question_mark_used)]
5+
6+
fn other_function() -> Option<i32> {
7+
Some(32)
8+
}
9+
10+
fn my_function() -> Option<i32> {
11+
other_function()?;
12+
None
13+
}
14+
15+
fn main() {}

tests/ui/question_mark_used.stderr

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: question mark operator was used
2+
--> $DIR/question_mark_used.rs:11:5
3+
|
4+
LL | other_function()?;
5+
| ^^^^^^^^^^^^^^^^^
6+
|
7+
= help: consider using a custom macro or match expression
8+
= note: `-D clippy::question-mark-used` implied by `-D warnings`
9+
10+
error: aborting due to previous error
11+

0 commit comments

Comments
 (0)