Skip to content

Commit

Permalink
fix(rsjudge-utils): ✅ fixed a potential misuse of log_if_error macro
Browse files Browse the repository at this point in the history
  • Loading branch information
Jisu-Woniu committed Apr 10, 2024
1 parent c4ee29c commit ed3f06b
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 2 deletions.
62 changes: 62 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions crates/rsjudge-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ rust-version.workspace = true
anyhow = "1.0.81"
log.workspace = true
shell-words = "1.1.0"

[dev-dependencies]
trybuild = "1.0.91"
2 changes: 1 addition & 1 deletion crates/rsjudge-utils/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use anyhow::{bail, ensure};
///
/// let mut cmd = Command::new("echo");
/// cmd.arg("Hello, world!");
/// assert_eq!(display_cmd(&cmd), "\"echo\" \"Hello, world!\"");
/// assert_eq!(display_cmd(&cmd), "echo 'Hello, world!'");
/// ```
pub fn display_cmd(cmd: &Command) -> String {
let args = iter::once(cmd.get_program())
Expand Down
2 changes: 1 addition & 1 deletion crates/rsjudge-utils/src/error_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#[macro_export]
macro_rules! log_if_error {
($expr: expr) => {
$expr.map_err(|err| {
::std::result::Result::map_err($expr, |err| {
::log::error!("{}", err);
err
})
Expand Down
22 changes: 22 additions & 0 deletions crates/rsjudge-utils/tests/logging_on_non_result.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
error[E0308]: mismatched types
--> tests/logging_on_non_result.test:19:27
|
19 | let _ = log_if_error!(S);
| --------------^-
| | |
| | expected `Result<_, _>`, found `S`
| arguments to this function are incorrect
|
= note: expected enum `Result<_, _>`
found struct `S`
note: method defined here
--> $RUST/core/src/result.rs
|
| pub fn map_err<F, O: FnOnce(E) -> F>(self, op: O) -> Result<T, F> {
| ^^^^^^^
help: try wrapping the expression in a variant of `Result`
|
19 | let _ = log_if_error!(Ok(S));
| +++ +
19 | let _ = log_if_error!(Err(S));
| ++++ +
20 changes: 20 additions & 0 deletions crates/rsjudge-utils/tests/logging_on_non_result.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use std::fmt::Display;

use rsjudge_utils::log_if_error;

fn main() {
#[derive(Debug)]
struct S;
impl Display for S {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "S")
}
}
impl S {
fn map_err<F: FnOnce(Self) -> Self>(self, f: F) -> Self {
f(self)
}
}

let _ = log_if_error!(S);
}
7 changes: 7 additions & 0 deletions crates/rsjudge-utils/tests/macro_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use trybuild::TestCases;

#[test]
fn tests() {
let test_cases = TestCases::new();
test_cases.compile_fail("tests/logging_on_non_result.test");
}

0 comments on commit ed3f06b

Please sign in to comment.