Skip to content

Commit bb8c875

Browse files
committed
WIP
1 parent 12ca363 commit bb8c875

File tree

10 files changed

+133
-2
lines changed

10 files changed

+133
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5684,6 +5684,7 @@ Released 2018-09-13
56845684
[`manual_string_new`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_string_new
56855685
[`manual_strip`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_strip
56865686
[`manual_swap`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_swap
5687+
[`manual_try`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_try
56875688
[`manual_try_fold`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_try_fold
56885689
[`manual_unwrap_or`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_unwrap_or
56895690
[`manual_unwrap_or_default`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_unwrap_or_default

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code.
77

8-
[There are over 700 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
8+
[There are over 750 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
99

1010
Lints are divided into categories, each with a default [lint level](https://doc.rust-lang.org/rustc/lints/levels.html).
1111
You can choose how much Clippy is supposed to ~~annoy~~ help you by changing the lint level by category.

book/src/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
A collection of lints to catch common mistakes and improve your
77
[Rust](https://github.com/rust-lang/rust) code.
88

9-
[There are over 700 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
9+
[There are over 750 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
1010

1111
Lints are divided into categories, each with a default [lint
1212
level](https://doc.rust-lang.org/rustc/lints/levels.html). You can choose how

cargo-clippy

2.03 MB
Binary file not shown.

clippy_config/src/conf.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,7 @@ define_Conf! {
570570
manual_split_once,
571571
manual_str_repeat,
572572
manual_strip,
573+
manual_try,
573574
manual_try_fold,
574575
map_clone,
575576
map_unwrap_or,

clippy_config/src/msrvs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ msrv_aliases! {
4646
1,42,0 { MATCHES_MACRO, SLICE_PATTERNS, PTR_SLICE_RAW_PARTS }
4747
1,41,0 { RE_REBALANCING_COHERENCE, RESULT_MAP_OR_ELSE }
4848
1,40,0 { MEM_TAKE, NON_EXHAUSTIVE, OPTION_AS_DEREF }
49+
1,39,0 { TRY_OPERATOR }
4950
1,38,0 { POINTER_CAST, REM_EUCLID }
5051
1,37,0 { TYPE_ALIAS_ENUM_VARIANTS }
5152
1,36,0 { ITERATOR_COPIED }

clippy_lints/src/declared_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ pub static LINTS: &[&crate::LintInfo] = &[
321321
crate::manual_slice_size_calculation::MANUAL_SLICE_SIZE_CALCULATION_INFO,
322322
crate::manual_string_new::MANUAL_STRING_NEW_INFO,
323323
crate::manual_strip::MANUAL_STRIP_INFO,
324+
crate::manual_try::MANUAL_TRY_INFO,
324325
crate::manual_unwrap_or_default::MANUAL_UNWRAP_OR_DEFAULT_INFO,
325326
crate::map_unit_fn::OPTION_MAP_UNIT_FN_INFO,
326327
crate::map_unit_fn::RESULT_MAP_UNIT_FN_INFO,

clippy_lints/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ extern crate rustc_session;
5454
extern crate rustc_span;
5555
extern crate rustc_target;
5656
extern crate rustc_trait_selection;
57+
extern crate smallvec;
5758
extern crate thin_vec;
5859

5960
#[macro_use]
@@ -218,6 +219,7 @@ mod manual_rotate;
218219
mod manual_slice_size_calculation;
219220
mod manual_string_new;
220221
mod manual_strip;
222+
mod manual_try;
221223
mod manual_unwrap_or_default;
222224
mod map_unit_fn;
223225
mod match_result_ok;
@@ -949,5 +951,6 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
949951
store.register_late_pass(move |_| Box::new(unused_trait_names::UnusedTraitNames::new(conf)));
950952
store.register_late_pass(|_| Box::new(manual_ignore_case_cmp::ManualIgnoreCaseCmp));
951953
store.register_late_pass(|_| Box::new(unnecessary_literal_bound::UnnecessaryLiteralBound));
954+
store.register_late_pass(move |_| Box::new(manual_try::ManualTry::new(conf)));
952955
// add lints here, do not remove this comment, it's used in `new_lint`
953956
}

clippy_lints/src/manual_try.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
use clippy_config::Conf;
2+
use clippy_config::msrvs::Msrv;
3+
use rustc_hir::intravisit::FnKind;
4+
use rustc_hir::{Body, Expr, ExprKind, FnDecl, FnRetTy, MatchSource, Node};
5+
use rustc_lint::{LateContext, LateLintPass};
6+
use rustc_session::impl_lint_pass;
7+
use rustc_span::Span;
8+
use rustc_span::def_id::LocalDefId;
9+
use smallvec::SmallVec;
10+
11+
declare_clippy_lint! {
12+
/// ### What it does
13+
///
14+
/// ### Why is this bad?
15+
///
16+
/// ### Example
17+
/// ```no_run
18+
/// // example code where clippy issues a warning
19+
/// ```
20+
/// Use instead:
21+
/// ```no_run
22+
/// // example code which does not raise clippy warning
23+
/// ```
24+
#[clippy::version = "1.84.0"]
25+
pub MANUAL_TRY,
26+
complexity,
27+
"default lint description"
28+
}
29+
30+
pub struct ManualTry<'tcx> {
31+
msrv: Msrv,
32+
fn_decls: SmallVec<[&'tcx FnDecl<'tcx>; 1]>,
33+
}
34+
35+
impl ManualTry<'_> {
36+
pub fn new(conf: &'static Conf) -> Self {
37+
Self {
38+
msrv: conf.msrv.clone(),
39+
fn_decls: SmallVec::new(),
40+
}
41+
}
42+
}
43+
44+
impl_lint_pass!(ManualTry<'_> => [MANUAL_TRY]);
45+
46+
impl<'tcx> LateLintPass<'tcx> for ManualTry<'tcx> {
47+
fn check_fn(
48+
&mut self,
49+
_: &LateContext<'_>,
50+
_: FnKind<'_>,
51+
decl: &'tcx FnDecl<'tcx>,
52+
_: &'_ Body<'_>,
53+
_: Span,
54+
_: LocalDefId,
55+
) {
56+
self.fn_decls.push(decl);
57+
}
58+
59+
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
60+
if let ExprKind::Match(_, [arm1, arm2], MatchSource::Normal | MatchSource::Postfix) = expr.kind && {}
61+
}
62+
63+
extract_msrv_attr!(LateContext);
64+
}
65+
66+
enum TryOpType {
67+
Result,
68+
Option,
69+
}

tests/ui/manual_try.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#![warn(clippy::manual_try)]
2+
3+
struct Error;
4+
5+
fn returns_res() -> Result<u8, Error> {
6+
Ok(1)
7+
}
8+
9+
fn returns_opt() -> Option<u8> {
10+
None
11+
}
12+
13+
#[clippy::msrv = "1.38"]
14+
fn below_msrv() -> Result<(), Error> {
15+
let _val = match returns_res() {
16+
Ok(val) => val,
17+
Err(err) => return Err(err),
18+
};
19+
20+
Ok(())
21+
}
22+
23+
#[clippy::msrv = "1.39"]
24+
fn at_msrv_res() -> Result<(), Error> {
25+
let _val = match returns_res() {
26+
Ok(val) => val,
27+
Err(err) => return Err(err),
28+
};
29+
//~^ error: manually implementing the `?` operator
30+
31+
match returns_res() {
32+
Ok(val) => val,
33+
Err(err) => return Err(err),
34+
};
35+
//~^ error: manually implementing the `?` operator
36+
37+
Ok(())
38+
}
39+
40+
#[clippy::msrv = "1.39"]
41+
fn at_msrv_opt() -> Option<u8> {
42+
let _val = match returns_opt() {
43+
Some(val) => val,
44+
None => return None,
45+
};
46+
//~^ error: manually implementing the `?` operator
47+
48+
match returns_opt() {
49+
Some(val) => val,
50+
None => return None,
51+
};
52+
//~^ error: manually implementing the `?` operator
53+
54+
Some(1)
55+
}

0 commit comments

Comments
 (0)