fork/rust

git clone https://git.julia.blue/fork/rust

borsAuto merge of #103046 - JanBeh:PR_clarify_cmp_terminology, r=workingjubilee3bcad65fbf6

master
2.8 KiB137 linesraw
1#![warn(clippy::needless_question_mark)]
2#![allow(
3    clippy::needless_return,
4    clippy::unnecessary_unwrap,
5    clippy::upper_case_acronyms,
6    dead_code,
7    unused_must_use
8)]
9
10struct TO {
11    magic: Option<usize>,
12}
13
14struct TR {
15    magic: Result<usize, bool>,
16}
17
18fn simple_option_bad1(to: TO) -> Option<usize> {
19    // return as a statement
20    return to.magic;
21}
22
23// formatting will add a semi-colon, which would make
24// this identical to the test case above
25#[rustfmt::skip]
26fn simple_option_bad2(to: TO) -> Option<usize> {
27    // return as an expression
28    return to.magic
29}
30
31fn simple_option_bad3(to: TO) -> Option<usize> {
32    // block value "return"
33    to.magic
34}
35
36fn simple_option_bad4(to: Option<TO>) -> Option<usize> {
37    // single line closure
38    to.and_then(|t| t.magic)
39}
40
41// formatting this will remove the block brackets, making
42// this test identical to the one above
43#[rustfmt::skip]
44fn simple_option_bad5(to: Option<TO>) -> Option<usize> {
45    // closure with body
46    to.and_then(|t| {
47        t.magic
48    })
49}
50
51fn simple_result_bad1(tr: TR) -> Result<usize, bool> {
52    return tr.magic;
53}
54
55// formatting will add a semi-colon, which would make
56// this identical to the test case above
57#[rustfmt::skip]
58fn simple_result_bad2(tr: TR) -> Result<usize, bool> {
59    return tr.magic
60}
61
62fn simple_result_bad3(tr: TR) -> Result<usize, bool> {
63    tr.magic
64}
65
66fn simple_result_bad4(tr: Result<TR, bool>) -> Result<usize, bool> {
67    tr.and_then(|t| t.magic)
68}
69
70// formatting this will remove the block brackets, making
71// this test identical to the one above
72#[rustfmt::skip]
73fn simple_result_bad5(tr: Result<TR, bool>) -> Result<usize, bool> {
74    tr.and_then(|t| {
75        t.magic
76    })
77}
78
79fn also_bad(tr: Result<TR, bool>) -> Result<usize, bool> {
80    if tr.is_ok() {
81        let t = tr.unwrap();
82        return t.magic;
83    }
84    Err(false)
85}
86
87fn false_positive_test<U, T>(x: Result<(), U>) -> Result<(), T>
88where
89    T: From<U>,
90{
91    Ok(x?)
92}
93
94// not quite needless
95fn deref_ref(s: Option<&String>) -> Option<&str> {
96    Some(s?)
97}
98
99fn main() {}
100
101// #6921 if a macro wraps an expr in Some(  ) and the ? is in the macro use,
102// the suggestion fails to apply; do not lint
103macro_rules! some_in_macro {
104    ($expr:expr) => {
105        || -> _ { Some($expr) }()
106    };
107}
108
109pub fn test1() {
110    let x = Some(3);
111    let _x = some_in_macro!(x?);
112}
113
114// this one is ok because both the ? and the Some are both inside the macro def
115macro_rules! some_and_qmark_in_macro {
116    ($expr:expr) => {
117        || -> Option<_> { Some($expr) }()
118    };
119}
120
121pub fn test2() {
122    let x = Some(3);
123    let _x = some_and_qmark_in_macro!(x?);
124}
125
126async fn async_option_bad(to: TO) -> Option<usize> {
127    let _ = Some(3);
128    to.magic
129}
130
131async fn async_deref_ref(s: Option<&String>) -> Option<&str> {
132    Some(s?)
133}
134
135async fn async_result_bad(s: TR) -> Result<usize, bool> {
136    s.magic
137}