Skip to content

Commit 8570af1

Browse files
authored
Add print_stdout and print_stderr lints (#17446) (#18233)
# Objective - Prevent usage of `println!`, `eprintln!` and the like because they require `std` - Fixes #17446 ## Solution - Enable the `print_stdout` and `print_stderr` clippy lints - Replace all `println!` and `eprintln!` occurrences with `log::*` where applicable or alternatively ignore the warnings ## Testing - Run `cargo clippy --workspace` to ensure that there are no warnings relating to printing to `stdout` or `stderr`
1 parent 4f62411 commit 8570af1

File tree

26 files changed

+58
-14
lines changed

26 files changed

+58
-14
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ unwrap_or_default = "warn"
5151
needless_lifetimes = "allow"
5252
too_many_arguments = "allow"
5353
nonstandard_macro_braces = "warn"
54+
print_stdout = "warn"
55+
print_stderr = "warn"
5456

5557
ptr_as_ptr = "warn"
5658
ptr_cast_constness = "warn"

crates/bevy_asset/src/asset_changed.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ unsafe impl<A: AsAssetId> QueryFilter for AssetChanged<A> {
281281
}
282282

283283
#[cfg(test)]
284+
#[expect(clippy::print_stdout, reason = "Allowed in tests.")]
284285
mod tests {
285286
use crate::{AssetEvents, AssetPlugin, Handle};
286287
use alloc::{vec, vec::Vec};

crates/bevy_ecs/examples/change_detection.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
99
#![expect(
1010
clippy::std_instead_of_core,
11+
clippy::print_stdout,
1112
reason = "Examples should not follow this lint"
1213
)]
1314

crates/bevy_ecs/examples/events.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! In this example a system sends a custom event with a 50/50 chance during any frame.
22
//! If an event was send, it will be printed by the console in a receiving system.
33
4+
#![expect(clippy::print_stdout, reason = "Allowed in examples.")]
5+
46
use bevy_ecs::{event::EventRegistry, prelude::*};
57

68
fn main() {

crates/bevy_ecs/examples/resources.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
44
#![expect(
55
clippy::std_instead_of_core,
6+
clippy::print_stdout,
67
reason = "Examples should not follow this lint"
78
)]
89

crates/bevy_ecs/src/error/bevy_error.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ std::thread_local! {
139139

140140
/// When called, this will skip the currently configured panic hook when a [`BevyError`] backtrace has already been printed.
141141
#[cfg(feature = "backtrace")]
142+
#[expect(clippy::print_stdout, reason = "Allowed behind `std` feature gate.")]
142143
pub fn bevy_error_panic_hook(
143144
current_hook: impl Fn(&std::panic::PanicHookInfo),
144145
) -> impl Fn(&std::panic::PanicHookInfo) {

crates/bevy_ecs/src/query/iter.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2585,6 +2585,7 @@ impl<T> Ord for NeutralOrd<T> {
25852585
}
25862586

25872587
#[cfg(test)]
2588+
#[expect(clippy::print_stdout, reason = "Allowed in tests.")]
25882589
mod tests {
25892590
use alloc::vec::Vec;
25902591
use std::println;

crates/bevy_ecs/src/query/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ impl<T> DebugCheckedUnwrap for Option<T> {
102102
}
103103

104104
#[cfg(test)]
105+
#[expect(clippy::print_stdout, reason = "Allowed in tests.")]
105106
mod tests {
106107
use crate::{
107108
archetype::Archetype,

crates/bevy_ecs/src/schedule/executor/multi_threaded.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ use bevy_utils::{default, syncunsafecell::SyncUnsafeCell};
55
use concurrent_queue::ConcurrentQueue;
66
use core::{any::Any, panic::AssertUnwindSafe};
77
use fixedbitset::FixedBitSet;
8-
use std::{
9-
eprintln,
10-
sync::{Mutex, MutexGuard},
11-
};
8+
#[cfg(feature = "std")]
9+
use std::eprintln;
10+
use std::sync::{Mutex, MutexGuard};
1211

1312
#[cfg(feature = "trace")]
1413
use tracing::{info_span, Span};
@@ -283,7 +282,11 @@ impl<'scope, 'env: 'scope, 'sys> Context<'scope, 'env, 'sys> {
283282
.push(SystemResult { system_index })
284283
.unwrap_or_else(|error| unreachable!("{}", error));
285284
if let Err(payload) = res {
286-
eprintln!("Encountered a panic in system `{}`!", &*system.name());
285+
#[cfg(feature = "std")]
286+
#[expect(clippy::print_stderr, reason = "Allowed behind `std` feature gate.")]
287+
{
288+
eprintln!("Encountered a panic in system `{}`!", &*system.name());
289+
}
287290
// set the payload to propagate the error
288291
{
289292
let mut panic_payload = self.environment.executor.panic_payload.lock().unwrap();
@@ -741,10 +744,14 @@ fn apply_deferred(
741744
system.apply_deferred(world);
742745
}));
743746
if let Err(payload) = res {
744-
eprintln!(
745-
"Encountered a panic when applying buffers for system `{}`!",
746-
&*system.name()
747-
);
747+
#[cfg(feature = "std")]
748+
#[expect(clippy::print_stderr, reason = "Allowed behind `std` feature gate.")]
749+
{
750+
eprintln!(
751+
"Encountered a panic when applying buffers for system `{}`!",
752+
&*system.name()
753+
);
754+
}
748755
return Err(payload);
749756
}
750757
}

crates/bevy_ecs/src/schedule/executor/simple.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ impl SystemExecutor for SimpleExecutor {
118118
});
119119

120120
#[cfg(feature = "std")]
121+
#[expect(clippy::print_stderr, reason = "Allowed behind `std` feature gate.")]
121122
{
122123
if let Err(payload) = std::panic::catch_unwind(f) {
123124
eprintln!("Encountered a panic in system `{}`!", &*system.name());

0 commit comments

Comments
 (0)