Skip to content

Commit 64dc8c6

Browse files
authored
clippy: dissallow some methods and types (#1411)
* Allow-list some words in our markdown docstrings * Forbid a bunch of macros, functions and types * name the threads * Do not ignore errors * Replace use of std::sync::Mutex with parking_lot::Mutex * Warn on failure to spawn analytics threads instead of panicing
1 parent 32d36d9 commit 64dc8c6

File tree

9 files changed

+89
-17
lines changed

9 files changed

+89
-17
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cranky.toml

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# https://github.com/ericseppanen/cargo-cranky
22
# cargo install cargo-cranky && cargo cranky
3+
# See also clippy.toml
34

45
deny = [
56
"unsafe_code",
@@ -17,8 +18,11 @@ warn = [
1718
"clippy::dbg_macro",
1819
"clippy::debug_assert_with_mut_call",
1920
"clippy::derive_partial_eq_without_eq",
20-
"clippy::disallowed_methods",
21-
"clippy::disallowed_script_idents",
21+
"clippy::disallowed_macros", # See clippy.toml
22+
"clippy::disallowed_methods", # See clippy.toml
23+
"clippy::disallowed_names", # See clippy.toml
24+
"clippy::disallowed_script_idents", # See clippy.toml
25+
"clippy::disallowed_types", # See clippy.toml
2226
"clippy::doc_link_with_quotes",
2327
"clippy::doc_markdown",
2428
"clippy::empty_enum",

clippy.toml

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
msrv = "1.67"
2+
3+
4+
# https://rust-lang.github.io/rust-clippy/master/index.html#disallowed_macros
5+
disallowed-macros = [
6+
'dbg',
7+
8+
# TODO(emilk): consider forbidding these to encourage the use of proper log stream, and then explicitly allow legitimate uses
9+
# 'std::eprint',
10+
# 'std::eprintln',
11+
# 'std::print',
12+
# 'std::println',
13+
14+
# 'std::unimplemented', # generated by ArrowDeserialize derive-macro :(
15+
]
16+
17+
# https://rust-lang.github.io/rust-clippy/master/index.html#disallowed_methods
18+
disallowed-methods = [
19+
"std::env::temp_dir", # Use the tempdir crate instead
20+
21+
# Disabled because of https://github.com/rust-lang/rust-clippy/issues/10406
22+
# "std::time::Instant::now", # use `instant` crate instead for wasm/web compatability
23+
"std::time::Duration::elapsed", # use `instant` crate instead for wasm/web compatability
24+
"std::time::SystemTime::now", # use `instant` or `time` crates instead for wasm/web compatability
25+
26+
"std::thread::spawn", # Use `std::thread::Builder` and name the thread
27+
28+
"sha1::Digest::new", # SHA1 is cryptographically broken
29+
]
30+
31+
# https://rust-lang.github.io/rust-clippy/master/index.html#disallowed_names
32+
disallowed-names = []
33+
34+
# https://rust-lang.github.io/rust-clippy/master/index.html#disallowed_types
35+
disallowed-types = [
36+
# Use the faster & simpler non-poisonable primitives in `parking_lot` instead
37+
"std::sync::Mutex",
38+
"std::sync::RwLock",
39+
"std::sync::Condvar",
40+
# "std::sync::Once", # enabled for now as the `log_once` macro uses it internally
41+
42+
"ring::digest::SHA1_FOR_LEGACY_USE_ONLY", # SHA1 is cryptographically broken
43+
]
44+
45+
# Allow-list of words for markdown in dosctrings https://rust-lang.github.io/rust-clippy/master/index.html#doc_markdown
46+
doc-valid-idents = [
47+
"GLB",
48+
"GLTF",
49+
"iOS",
50+
"macOS",
51+
"NaN",
52+
"OBJ",
53+
"sRGB",
54+
"sRGBA",
55+
"WebGL",
56+
]

crates/re_analytics/src/pipeline_native.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl Pipeline {
7272
// The eventual part comes from the fact that this only runs as part of the Rerun viewer,
7373
// and as such there's no guarantee it will ever run again, even if there's pending data.
7474

75-
_ = std::thread::Builder::new()
75+
if let Err(err) = std::thread::Builder::new()
7676
.name("pipeline_catchup".into())
7777
.spawn({
7878
let config = config.clone();
@@ -85,9 +85,12 @@ impl Pipeline {
8585
let res = flush_pending_events(&config, &sink);
8686
trace!(%analytics_id, %session_id, ?res, "pipeline catchup thread shut down");
8787
}
88-
});
88+
})
89+
{
90+
re_log::warn!("Failed to spawn analytics thread: {err}");
91+
}
8992

90-
_ = std::thread::Builder::new().name("pipeline".into()).spawn({
93+
if let Err(err) = std::thread::Builder::new().name("pipeline".into()).spawn({
9194
let config = config.clone();
9295
let event_tx = event_tx.clone();
9396
move || {
@@ -99,7 +102,9 @@ impl Pipeline {
99102
realtime_pipeline(&config, &sink, session_file, tick, &event_tx, &event_rx);
100103
trace!(%analytics_id, %session_id, ?res, "pipeline thread shut down");
101104
}
102-
});
105+
}) {
106+
re_log::warn!("Failed to spawn analytics thread: {err}");
107+
}
103108

104109
Ok(Some(Self { event_tx }))
105110
}
@@ -266,7 +271,7 @@ fn append_event(
266271
// corrupt row in the analytics file, that we'll simply discard later on.
267272
// We'll try to write a linefeed one more time, just in case, to avoid potentially
268273
// impacting other events.
269-
_ = session_file.write_all(b"\n");
274+
session_file.write_all(b"\n").ok();
270275
warn!(%err, %analytics_id, %session_id, "couldn't write to analytics data file");
271276
return Err(event);
272277
}

crates/re_log_types/src/component_types/mesh3d.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ impl ArrowDeserialize for EncodedMesh3D {
326326

327327
// ----------------------------------------------------------------------------
328328

329-
/// The format of a binary mesh file, e.g. `GLTF`, `GLB`, `OBJ`
329+
/// The format of a binary mesh file, e.g. GLTF, GLB, OBJ
330330
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, ArrowField, ArrowSerialize, ArrowDeserialize)]
331331
#[arrow_field(type = "dense")]
332332
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]

crates/re_sdk/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ re_sdk_comms = { workspace = true, features = ["client"] }
5454
re_smart_channel.workspace = true
5555
re_string_interner.workspace = true
5656

57-
5857
anyhow.workspace = true
5958
arrow2.workspace = true
6059
crossbeam = "0.8"
6160
document-features = "0.2"
6261
lazy_static.workspace = true
6362
nohash-hasher = "0.2"
6463
once_cell = "1.12"
64+
parking_lot = "0.12"
6565
thiserror.workspace = true
6666

6767
# Optional dependencies:

crates/re_sdk/src/global.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::session::Session;
44
///
55
/// By default, logging is enabled. To disable logging, call `set_enabled(false)` on the global `Session`, or
66
/// set the `RERUN` environment variable to `false`.
7-
pub fn global_session() -> std::sync::MutexGuard<'static, Session> {
7+
pub fn global_session() -> parking_lot::MutexGuard<'static, Session> {
88
let default_enabled = true;
99
global_session_with_default_enabled(default_enabled)
1010
}
@@ -15,11 +15,11 @@ pub fn global_session() -> std::sync::MutexGuard<'static, Session> {
1515
/// It can be overridden with the `RERUN` environment variable.
1616
pub fn global_session_with_default_enabled(
1717
default_enabled: bool,
18-
) -> std::sync::MutexGuard<'static, Session> {
18+
) -> parking_lot::MutexGuard<'static, Session> {
1919
use once_cell::sync::OnceCell;
20-
use std::sync::Mutex;
20+
use parking_lot::Mutex;
2121
static INSTANCE: OnceCell<Mutex<Session>> = OnceCell::new();
2222

2323
let mutex = INSTANCE.get_or_init(|| Mutex::new(Session::with_default_enabled(default_enabled)));
24-
mutex.lock().unwrap()
24+
mutex.lock()
2525
}

crates/re_sdk/src/session.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,10 @@ impl Session {
405405
let app_env = self.app_env();
406406

407407
// NOTE: Forget the handle on purpose, leave that thread be.
408-
_ = std::thread::spawn(move || run(self));
408+
std::thread::Builder::new()
409+
.name("spawned".into())
410+
.spawn(move || run(self))
411+
.expect("Failed to spawn thread");
409412

410413
// NOTE: Some platforms still mandate that the UI must run on the main thread, so make sure
411414
// to spawn the viewer in place and migrate the user callback to a new thread.

run_wasm/src/main.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,12 @@ fn main() {
9999
let host = host.as_deref().unwrap_or("localhost");
100100
let port = port.as_deref().unwrap_or("8000");
101101

102-
std::thread::spawn(|| {
103-
cargo_run_wasm::run_wasm_with_css(CSS);
104-
});
102+
std::thread::Builder::new()
103+
.name("cargo_run_wasm".into())
104+
.spawn(|| {
105+
cargo_run_wasm::run_wasm_with_css(CSS);
106+
})
107+
.expect("Failed to spawn thread");
105108

106109
// Wait for the server to be up before opening a browser tab.
107110
let addr = format!("{host}:{port}")

0 commit comments

Comments
 (0)