Skip to content

Commit 52c8b53

Browse files
committed
Auto merge of rust-lang#10342 - mitsuhiko:feature/question-mark-used, r=Alexendoo
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. Fixes rust-lang#10340 --- changelog: New lint [`question_mark_used`] [rust-lang#10342](rust-lang/rust-clippy#10342) <!-- changelog_checked -->
2 parents 99d4ea4 + 0933798 commit 52c8b53

File tree

6 files changed

+82
-0
lines changed

6 files changed

+82
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4676,6 +4676,7 @@ Released 2018-09-13
46764676
[`pub_enum_variant_names`]: https://rust-lang.github.io/rust-clippy/master/index.html#pub_enum_variant_names
46774677
[`pub_use`]: https://rust-lang.github.io/rust-clippy/master/index.html#pub_use
46784678
[`question_mark`]: https://rust-lang.github.io/rust-clippy/master/index.html#question_mark
4679+
[`question_mark_used`]: https://rust-lang.github.io/rust-clippy/master/index.html#question_mark_used
46794680
[`range_minus_one`]: https://rust-lang.github.io/rust-clippy/master/index.html#range_minus_one
46804681
[`range_plus_one`]: https://rust-lang.github.io/rust-clippy/master/index.html#range_plus_one
46814682
[`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
@@ -508,6 +508,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
508508
crate::ptr_offset_with_cast::PTR_OFFSET_WITH_CAST_INFO,
509509
crate::pub_use::PUB_USE_INFO,
510510
crate::question_mark::QUESTION_MARK_INFO,
511+
crate::question_mark_used::QUESTION_MARK_USED_INFO,
511512
crate::ranges::MANUAL_RANGE_CONTAINS_INFO,
512513
crate::ranges::RANGE_MINUS_ONE_INFO,
513514
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
@@ -243,6 +243,7 @@ mod ptr;
243243
mod ptr_offset_with_cast;
244244
mod pub_use;
245245
mod question_mark;
246+
mod question_mark_used;
246247
mod ranges;
247248
mod rc_clone_in_vec_init;
248249
mod read_zero_byte_vec;
@@ -696,6 +697,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
696697
store.register_late_pass(|_| Box::new(implicit_hasher::ImplicitHasher));
697698
store.register_late_pass(|_| Box::new(fallible_impl_from::FallibleImplFrom));
698699
store.register_late_pass(|_| Box::new(question_mark::QuestionMark));
700+
store.register_late_pass(|_| Box::new(question_mark_used::QuestionMarkUsed));
699701
store.register_early_pass(|| Box::new(suspicious_operation_groupings::SuspiciousOperationGroupings));
700702
store.register_late_pass(|_| Box::new(suspicious_trait_impl::SuspiciousImpl));
701703
store.register_late_pass(|_| Box::new(map_unit_fn::MapUnit));
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
use clippy_utils::diagnostics::span_lint_and_help;
2+
3+
use clippy_utils::macros::span_is_local;
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+
impl<'tcx> LateLintPass<'tcx> for QuestionMarkUsed {
36+
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
37+
if let ExprKind::Match(_, _, MatchSource::TryDesugar) = expr.kind {
38+
if !span_is_local(expr.span) {
39+
return;
40+
}
41+
42+
span_lint_and_help(
43+
cx,
44+
QUESTION_MARK_USED,
45+
expr.span,
46+
"question mark operator was used",
47+
None,
48+
"consider using a custom macro or match expression",
49+
);
50+
}
51+
}
52+
}

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)