Skip to content

Commit beee858

Browse files
committed
Rename [misleading_use_of_ok] to [unused_result_ok] and add macro tests
1 parent 256291b commit beee858

10 files changed

+173
-68
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5347,7 +5347,6 @@ Released 2018-09-13
53475347
[`min_ident_chars`]: https://rust-lang.github.io/rust-clippy/master/index.html#min_ident_chars
53485348
[`min_max`]: https://rust-lang.github.io/rust-clippy/master/index.html#min_max
53495349
[`misaligned_transmute`]: https://rust-lang.github.io/rust-clippy/master/index.html#misaligned_transmute
5350-
[`misleading_use_of_ok`]: https://rust-lang.github.io/rust-clippy/master/index.html#misleading_use_of_ok
53515350
[`mismatched_target_os`]: https://rust-lang.github.io/rust-clippy/master/index.html#mismatched_target_os
53525351
[`mismatching_type_param_order`]: https://rust-lang.github.io/rust-clippy/master/index.html#mismatching_type_param_order
53535352
[`misnamed_getters`]: https://rust-lang.github.io/rust-clippy/master/index.html#misnamed_getters
@@ -5704,6 +5703,7 @@ Released 2018-09-13
57045703
[`unused_io_amount`]: https://rust-lang.github.io/rust-clippy/master/index.html#unused_io_amount
57055704
[`unused_label`]: https://rust-lang.github.io/rust-clippy/master/index.html#unused_label
57065705
[`unused_peekable`]: https://rust-lang.github.io/rust-clippy/master/index.html#unused_peekable
5706+
[`unused_result_ok`]: https://rust-lang.github.io/rust-clippy/master/index.html#unused_result_ok
57075707
[`unused_rounding`]: https://rust-lang.github.io/rust-clippy/master/index.html#unused_rounding
57085708
[`unused_self`]: https://rust-lang.github.io/rust-clippy/master/index.html#unused_self
57095709
[`unused_unit`]: https://rust-lang.github.io/rust-clippy/master/index.html#unused_unit

clippy_lints/src/declared_lints.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,6 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
477477
crate::misc_early::UNNEEDED_WILDCARD_PATTERN_INFO,
478478
crate::misc_early::UNSEPARATED_LITERAL_SUFFIX_INFO,
479479
crate::misc_early::ZERO_PREFIXED_LITERAL_INFO,
480-
crate::misleading_use_of_ok::MISLEADING_USE_OF_OK_INFO,
481480
crate::mismatching_type_param_order::MISMATCHING_TYPE_PARAM_ORDER_INFO,
482481
crate::missing_assert_message::MISSING_ASSERT_MESSAGE_INFO,
483482
crate::missing_asserts_for_indexing::MISSING_ASSERTS_FOR_INDEXING_INFO,
@@ -711,6 +710,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
711710
crate::unused_async::UNUSED_ASYNC_INFO,
712711
crate::unused_io_amount::UNUSED_IO_AMOUNT_INFO,
713712
crate::unused_peekable::UNUSED_PEEKABLE_INFO,
713+
crate::unused_result_ok::UNUSED_RESULT_OK_INFO,
714714
crate::unused_rounding::UNUSED_ROUNDING_INFO,
715715
crate::unused_self::UNUSED_SELF_INFO,
716716
crate::unused_unit::UNUSED_UNIT_INFO,

clippy_lints/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,6 @@ mod min_ident_chars;
212212
mod minmax;
213213
mod misc;
214214
mod misc_early;
215-
mod misleading_use_of_ok;
216215
mod mismatching_type_param_order;
217216
mod missing_assert_message;
218217
mod missing_asserts_for_indexing;
@@ -350,6 +349,7 @@ mod unsafe_removed_from_name;
350349
mod unused_async;
351350
mod unused_io_amount;
352351
mod unused_peekable;
352+
mod unused_result_ok;
353353
mod unused_rounding;
354354
mod unused_self;
355355
mod unused_unit;
@@ -766,7 +766,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
766766
store.register_late_pass(move |_| Box::new(missing_doc::MissingDoc::new(missing_docs_in_crate_items)));
767767
store.register_late_pass(|_| Box::new(missing_inline::MissingInline));
768768
store.register_late_pass(move |_| Box::new(exhaustive_items::ExhaustiveItems));
769-
store.register_late_pass(|_| Box::new(misleading_use_of_ok::MisleadingUseOfOk));
769+
store.register_late_pass(|_| Box::new(unused_result_ok::UnusedResultOk));
770770
store.register_late_pass(|_| Box::new(match_result_ok::MatchResultOk));
771771
store.register_late_pass(|_| Box::new(partialeq_ne_impl::PartialEqNeImpl));
772772
store.register_late_pass(|_| Box::new(unused_io_amount::UnusedIoAmount));

clippy_lints/src/misleading_use_of_ok.rs clippy_lints/src/unused_result_ok.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ declare_clippy_lint! {
2727
/// let _ = some_function();
2828
/// ```
2929
#[clippy::version = "1.70.0"]
30-
pub MISLEADING_USE_OF_OK,
30+
pub UNUSED_RESULT_OK,
3131
style,
3232
"Use of `.ok()` to silence `Result`'s `#[must_use]` is misleading. Use `let _ =` instead."
3333
}
34-
declare_lint_pass!(MisleadingUseOfOk => [MISLEADING_USE_OF_OK]);
34+
declare_lint_pass!(UnusedResultOk => [UNUSED_RESULT_OK]);
3535

36-
impl LateLintPass<'_> for MisleadingUseOfOk {
36+
impl LateLintPass<'_> for UnusedResultOk {
3737
fn check_stmt(&mut self, cx: &LateContext<'_>, stmt: &Stmt<'_>) {
3838
if let StmtKind::Semi(expr) = stmt.kind &&
3939
let ExprKind::MethodCall(ok_path, recv, [], ..) = expr.kind //check is expr.ok() has type Result<T,E>.ok(, _)
@@ -47,7 +47,7 @@ impl LateLintPass<'_> for MisleadingUseOfOk {
4747
let sugg = format!("let _ = {snippet}");
4848
span_lint_and_sugg(
4949
cx,
50-
MISLEADING_USE_OF_OK,
50+
UNUSED_RESULT_OK,
5151
expr.span,
5252
"ignoring a result with `.ok()` is misleading",
5353
"consider using `let _ =` and removing the call to `.ok()` instead",

tests/ui/misleading_use_of_ok.fixed

-17
This file was deleted.

tests/ui/misleading_use_of_ok.rs

-17
This file was deleted.

tests/ui/misleading_use_of_ok.stderr

-26
This file was deleted.

tests/ui/unused_result_ok.fixed

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//@aux-build:proc_macros.rs
2+
#![warn(clippy::unused_result_ok)]
3+
#![allow(dead_code)]
4+
5+
#[macro_use]
6+
extern crate proc_macros;
7+
8+
fn bad_style(x: &str) {
9+
let _ = x.parse::<u32>();
10+
}
11+
12+
fn good_style(x: &str) -> Option<u32> {
13+
x.parse::<u32>().ok()
14+
}
15+
16+
#[rustfmt::skip]
17+
fn strange_parse(x: &str) {
18+
let _ = x . parse::<i32>();
19+
}
20+
21+
macro_rules! v {
22+
() => {
23+
Ok::<(), ()>(())
24+
};
25+
}
26+
27+
macro_rules! w {
28+
() => {
29+
let _ = Ok::<(), ()>(());
30+
};
31+
}
32+
33+
fn main() {
34+
let _ = v!();
35+
w!();
36+
37+
external! {
38+
macro_rules! x {
39+
() => { Ok::<(), ()>(()) }
40+
}
41+
macro_rules! y {
42+
() => { Ok::<(), ()>(()).ok(); }
43+
}
44+
45+
x!().ok();
46+
y!();
47+
};
48+
49+
let _ = x!();
50+
y!();
51+
}

tests/ui/unused_result_ok.rs

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//@aux-build:proc_macros.rs
2+
#![warn(clippy::unused_result_ok)]
3+
#![allow(dead_code)]
4+
5+
#[macro_use]
6+
extern crate proc_macros;
7+
8+
fn bad_style(x: &str) {
9+
x.parse::<u32>().ok();
10+
}
11+
12+
fn good_style(x: &str) -> Option<u32> {
13+
x.parse::<u32>().ok()
14+
}
15+
16+
#[rustfmt::skip]
17+
fn strange_parse(x: &str) {
18+
x . parse::<i32>() . ok ();
19+
}
20+
21+
macro_rules! v {
22+
() => {
23+
Ok::<(), ()>(())
24+
};
25+
}
26+
27+
macro_rules! w {
28+
() => {
29+
Ok::<(), ()>(()).ok();
30+
};
31+
}
32+
33+
fn main() {
34+
v!().ok();
35+
w!();
36+
37+
external! {
38+
macro_rules! x {
39+
() => { Ok::<(), ()>(()) }
40+
}
41+
macro_rules! y {
42+
() => { Ok::<(), ()>(()).ok(); }
43+
}
44+
45+
x!().ok();
46+
y!();
47+
};
48+
49+
x!().ok();
50+
y!();
51+
}

tests/ui/unused_result_ok.stderr

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
error: ignoring a result with `.ok()` is misleading
2+
--> $DIR/unused_result_ok.rs:9:5
3+
|
4+
LL | x.parse::<u32>().ok();
5+
| ^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::unused-result-ok` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::unused_result_ok)]`
9+
help: consider using `let _ =` and removing the call to `.ok()` instead
10+
|
11+
LL | let _ = x.parse::<u32>();
12+
| ~~~~~~~~~~~~~~~~~~~~~~~~
13+
14+
error: ignoring a result with `.ok()` is misleading
15+
--> $DIR/unused_result_ok.rs:18:5
16+
|
17+
LL | x . parse::<i32>() . ok ();
18+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
19+
|
20+
help: consider using `let _ =` and removing the call to `.ok()` instead
21+
|
22+
LL | let _ = x . parse::<i32>();
23+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24+
25+
error: ignoring a result with `.ok()` is misleading
26+
--> $DIR/unused_result_ok.rs:34:5
27+
|
28+
LL | v!().ok();
29+
| ^^^^^^^^^
30+
|
31+
help: consider using `let _ =` and removing the call to `.ok()` instead
32+
|
33+
LL | let _ = v!();
34+
| ~~~~~~~~~~~~
35+
36+
error: ignoring a result with `.ok()` is misleading
37+
--> $DIR/unused_result_ok.rs:29:9
38+
|
39+
LL | Ok::<(), ()>(()).ok();
40+
| ^^^^^^^^^^^^^^^^^^^^^
41+
...
42+
LL | w!();
43+
| ---- in this macro invocation
44+
|
45+
= note: this error originates in the macro `w` (in Nightly builds, run with -Z macro-backtrace for more info)
46+
help: consider using `let _ =` and removing the call to `.ok()` instead
47+
|
48+
LL | let _ = Ok::<(), ()>(());
49+
| ~~~~~~~~~~~~~~~~~~~~~~~~
50+
51+
error: ignoring a result with `.ok()` is misleading
52+
--> $DIR/unused_result_ok.rs:49:5
53+
|
54+
LL | x!().ok();
55+
| ^^^^^^^^^
56+
|
57+
help: consider using `let _ =` and removing the call to `.ok()` instead
58+
|
59+
LL | let _ = x!();
60+
| ~~~~~~~~~~~~
61+
62+
error: aborting due to 5 previous errors
63+

0 commit comments

Comments
 (0)