Skip to content

Commit

Permalink
Lint error names in map_err (#1948)
Browse files Browse the repository at this point in the history
* Lint: Properly name errors in `map_err`

* Use correct names for errors
  • Loading branch information
emilk authored Apr 20, 2023
1 parent 0116c50 commit f5e0e4d
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 9 deletions.
6 changes: 3 additions & 3 deletions crates/re_log_types/src/arrow_msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ impl serde::Serialize for ArrowMsg {
let mut writer = StreamWriter::new(&mut buf, Default::default());
writer
.start(&self.schema, None)
.map_err(|e| serde::ser::Error::custom(e.to_string()))?;
.map_err(|err| serde::ser::Error::custom(err.to_string()))?;
writer
.write(&self.chunk, None)
.map_err(|e| serde::ser::Error::custom(e.to_string()))?;
.map_err(|err| serde::ser::Error::custom(err.to_string()))?;
writer
.finish()
.map_err(|e| serde::ser::Error::custom(e.to_string()))?;
.map_err(|err| serde::ser::Error::custom(err.to_string()))?;

let mut inner = serializer.serialize_tuple(3)?;
inner.serialize_element(&self.table_id)?;
Expand Down
2 changes: 1 addition & 1 deletion crates/re_web_viewer_server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ impl WebViewerServer {
pub fn new(port: WebViewerServerPort) -> Result<Self, WebViewerServerError> {
let bind_addr = format!("0.0.0.0:{port}").parse()?;
let server = hyper::Server::try_bind(&bind_addr)
.map_err(|e| WebViewerServerError::BindFailed(port, e))?
.map_err(|err| WebViewerServerError::BindFailed(port, err))?
.serve(MakeSvc);
Ok(Self { server })
}
Expand Down
2 changes: 1 addition & 1 deletion crates/re_ws_comms/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl RerunServer {

let listener = TcpListener::bind(&bind_addr)
.await
.map_err(|e| RerunServerError::BindFailed(port, e))?;
.map_err(|err| RerunServerError::BindFailed(port, err))?;

let port = RerunServerPort(listener.local_addr()?.port());

Expand Down
2 changes: 1 addition & 1 deletion crates/rerun/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ async fn run_impl(
app.set_profiler(profiler);
Box::new(app)
}))
.map_err(|e| e.into());
.map_err(|err| err.into());

#[cfg(not(feature = "native_viewer"))]
{
Expand Down
4 changes: 2 additions & 2 deletions rerun_py/src/arrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn array_to_rust(arrow_array: &PyAny, name: Option<&str>) -> PyResult<(Box<dyn A
// Following pattern from: https://github.com/pola-rs/polars/blob/master/examples/python_rust_compiled_function/src/ffi.rs
unsafe {
let mut field = ffi::import_field_from_c(schema.as_ref())
.map_err(|e| PyValueError::new_err(format!("Error importing Field: {e}")))?;
.map_err(|err| PyValueError::new_err(format!("Error importing Field: {err}")))?;

// There is a bad incompatibility between pyarrow and arrow2-convert
// Force the type to be correct.
Expand All @@ -49,7 +49,7 @@ fn array_to_rust(arrow_array: &PyAny, name: Option<&str>) -> PyResult<(Box<dyn A
}

let array = ffi::import_array_from_c(*array, field.data_type.clone())
.map_err(|e| PyValueError::new_err(format!("Error importing Array: {e}")))?;
.map_err(|err| PyValueError::new_err(format!("Error importing Array: {err}")))?;

if let Some(name) = name {
field.name = name.to_owned();
Expand Down
8 changes: 7 additions & 1 deletion scripts/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
todo_pattern = re.compile(r"TODO([^(]|$)")
debug_format_of_err = re.compile(r"\{\:#?\?\}.*, err")
error_match_name = re.compile(r"Err\((\w+)\)")
error_map_err_name = re.compile(r"map_err\(\|(\w+)\|")
wasm_caps = re.compile(r"\bWASM\b")
nb_prefix = re.compile(r"nb_")

Expand Down Expand Up @@ -52,7 +53,8 @@ def lint_line(line: str) -> Optional[str]:
if "{err:?}" in line or "{err:#?}" in line or debug_format_of_err.search(line):
return "Format errors with re_error::format or using Display - NOT Debug formatting!"

if m := re.search(error_match_name, line):
m = re.search(error_map_err_name, line) or re.search(error_match_name, line)
if m:
name = m.group(1)
# if name not in ("err", "_err", "_"):
if name in ("e", "error"):
Expand Down Expand Up @@ -82,6 +84,9 @@ def test_lint_line() -> None:
"if let Err(err) = foo",
"if let Err(_err) = foo",
"if let Err(_) = foo",
"map_err(|err| …)",
"map_err(|_err| …)",
"map_err(|_| …)",
"WASM_FOO env var",
"Wasm",
"num_instances",
Expand All @@ -100,6 +105,7 @@ def test_lint_line() -> None:
'eprintln!("{:?}", err)',
'eprintln!("{:#?}", err)',
"if let Err(error) = foo",
"map_err(|e| …)",
"We use WASM in Rerun",
"nb_instances",
"inner_nb_instances",
Expand Down

0 comments on commit f5e0e4d

Please sign in to comment.