Skip to content

Commit

Permalink
address comment about macro invokation
Browse files Browse the repository at this point in the history
Signed-off-by: tabokie <[email protected]>
  • Loading branch information
tabokie committed Jul 25, 2022
1 parent 53a4b7a commit 7b2a0fa
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
11 changes: 6 additions & 5 deletions clippy_lints/src/assertions_on_result_states.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::macros::{find_assert_args, root_macro_call_first_node, PanicExpn};
use clippy_utils::path_res;
use clippy_utils::source::snippet_opt;
use clippy_utils::source::snippet_with_context;
use clippy_utils::ty::{implements_trait, is_copy, is_type_diagnostic_item};
use clippy_utils::usage::local_used_after_expr;
use rustc_errors::Applicability;
Expand Down Expand Up @@ -54,6 +54,7 @@ impl<'tcx> LateLintPass<'tcx> for AssertionsOnResultStates {
return;
}
}
let mut app = Applicability::MachineApplicable;
match method_segment.ident.as_str() {
"is_ok" if has_debug_impl(cx, substs.type_at(1)) => {
span_lint_and_sugg(
Expand All @@ -64,9 +65,9 @@ impl<'tcx> LateLintPass<'tcx> for AssertionsOnResultStates {
"replace with",
format!(
"{}.unwrap()",
snippet_opt(cx, recv.span).unwrap()
snippet_with_context(cx, recv.span, condition.span.ctxt(), "..", &mut app).0
),
Applicability::MachineApplicable,
app,
);
}
"is_err" if has_debug_impl(cx, substs.type_at(0)) => {
Expand All @@ -78,9 +79,9 @@ impl<'tcx> LateLintPass<'tcx> for AssertionsOnResultStates {
"replace with",
format!(
"{}.unwrap_err()",
snippet_opt(cx, recv.span).unwrap()
snippet_with_context(cx, recv.span, condition.span.ctxt(), "..", &mut app).0
),
Applicability::MachineApplicable,
app,
);
}
_ => (),
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/assertions_on_result_states.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ struct DebugFoo;
#[derive(Copy, Clone, Debug)]
struct CopyFoo;

macro_rules! get_ok_macro {
() => {
Ok::<_, DebugFoo>(Foo)
};
}

fn main() {
// test ok
let r: Result<Foo, DebugFoo> = Ok(Foo);
Expand All @@ -27,6 +33,9 @@ fn main() {
}
get_ok().unwrap();

// test macro ok
get_ok_macro!().unwrap();

// test ok that shouldn't be moved
let r: Result<CopyFoo, DebugFoo> = Ok(CopyFoo);
fn test_ref_unmoveable_ok(r: &Result<CopyFoo, DebugFoo>) {
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/assertions_on_result_states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ struct DebugFoo;
#[derive(Copy, Clone, Debug)]
struct CopyFoo;

macro_rules! get_ok_macro {
() => {
Ok::<_, DebugFoo>(Foo)
};
}

fn main() {
// test ok
let r: Result<Foo, DebugFoo> = Ok(Foo);
Expand All @@ -27,6 +33,9 @@ fn main() {
}
assert!(get_ok().is_ok());

// test macro ok
assert!(get_ok_macro!().is_ok());

// test ok that shouldn't be moved
let r: Result<CopyFoo, DebugFoo> = Ok(CopyFoo);
fn test_ref_unmoveable_ok(r: &Result<CopyFoo, DebugFoo>) {
Expand Down
18 changes: 12 additions & 6 deletions tests/ui/assertions_on_result_states.stderr
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
error: called `assert!` with `Result::is_ok`
--> $DIR/assertions_on_result_states.rs:18:5
--> $DIR/assertions_on_result_states.rs:24:5
|
LL | assert!(r.is_ok());
| ^^^^^^^^^^^^^^^^^^ help: replace with: `r.unwrap()`
|
= note: `-D clippy::assertions-on-result-states` implied by `-D warnings`

error: called `assert!` with `Result::is_ok`
--> $DIR/assertions_on_result_states.rs:28:5
--> $DIR/assertions_on_result_states.rs:34:5
|
LL | assert!(get_ok().is_ok());
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `get_ok().unwrap()`

error: called `assert!` with `Result::is_ok`
--> $DIR/assertions_on_result_states.rs:41:5
--> $DIR/assertions_on_result_states.rs:37:5
|
LL | assert!(get_ok_macro!().is_ok());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `get_ok_macro!().unwrap()`

error: called `assert!` with `Result::is_ok`
--> $DIR/assertions_on_result_states.rs:50:5
|
LL | assert!(r.is_ok());
| ^^^^^^^^^^^^^^^^^^ help: replace with: `r.unwrap()`

error: called `assert!` with `Result::is_ok`
--> $DIR/assertions_on_result_states.rs:47:9
--> $DIR/assertions_on_result_states.rs:56:9
|
LL | assert!(r.is_ok());
| ^^^^^^^^^^^^^^^^^^ help: replace with: `r.unwrap()`

error: called `assert!` with `Result::is_err`
--> $DIR/assertions_on_result_states.rs:55:5
--> $DIR/assertions_on_result_states.rs:64:5
|
LL | assert!(r.is_err());
| ^^^^^^^^^^^^^^^^^^^ help: replace with: `r.unwrap_err()`

error: aborting due to 5 previous errors
error: aborting due to 6 previous errors

0 comments on commit 7b2a0fa

Please sign in to comment.