Skip to content

Commit 4598185

Browse files
unixziilucasfernog
andauthored
chore(lint): unused variable warnings for release builds (#4411)
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
1 parent 12f6521 commit 4598185

File tree

4 files changed

+54
-32
lines changed

4 files changed

+54
-32
lines changed

.changes/unused-vars.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"tauri-utils": patch
3+
"tauri-runtime-wry": patch
4+
"tauri": patch
5+
---
6+
7+
Suppress unused variable warning in release builds.

core/tauri-runtime-wry/src/lib.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use wry::application::platform::windows::{WindowBuilderExtWindows, WindowExtWind
3939
#[cfg(feature = "system-tray")]
4040
use wry::application::system_tray::{SystemTray as WrySystemTray, SystemTrayBuilder};
4141

42-
use tauri_utils::{config::WindowConfig, Theme};
42+
use tauri_utils::{config::WindowConfig, debug_eprintln, Theme};
4343
use uuid::Uuid;
4444
use wry::{
4545
application::{
@@ -2435,8 +2435,7 @@ fn handle_user_message<T: UserEvent>(
24352435
.and_then(|w| w.inner.as_ref())
24362436
{
24372437
if let Err(e) = webview.evaluate_script(&script) {
2438-
#[cfg(debug_assertions)]
2439-
eprintln!("{}", e);
2438+
debug_eprintln!("{}", e);
24402439
}
24412440
}
24422441
}
@@ -2474,8 +2473,7 @@ fn handle_user_message<T: UserEvent>(
24742473
.insert(window_id, webview);
24752474
}
24762475
Err(e) => {
2477-
#[cfg(debug_assertions)]
2478-
eprintln!("{}", e);
2476+
debug_eprintln!("{}", e);
24792477
}
24802478
},
24812479
Message::CreateWindow(window_id, handler, sender) => {
@@ -2772,8 +2770,7 @@ fn handle_event_loop<T: UserEvent>(
27722770
.and_then(|w| w.inner.as_ref())
27732771
{
27742772
if let Err(e) = webview.resize() {
2775-
#[cfg(debug_assertions)]
2776-
eprintln!("{}", e);
2773+
debug_eprintln!("{}", e);
27772774
}
27782775
}
27792776
}

core/tauri-utils/src/lib.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,31 @@ pub enum Error {
199199
#[error("could not walk directory `{0}`, try changing `allow_walk` to true on the `ResourcePaths` constructor.")]
200200
NotAllowedToWalkDir(std::path::PathBuf),
201201
}
202+
203+
/// Suppresses the unused-variable warnings of the given inputs.
204+
///
205+
/// This does not move any values. Instead, it just suppresses the warning by taking a
206+
/// reference to the value.
207+
#[macro_export]
208+
macro_rules! consume_unused_variable {
209+
($($arg:expr),*) => {
210+
$(
211+
let _ = &$arg;
212+
)*
213+
()
214+
};
215+
}
216+
217+
/// Prints to the standard error, with a newline.
218+
///
219+
/// Equivalent to the [`eprintln!`] macro, except that it's only effective for debug builds.
220+
#[macro_export]
221+
macro_rules! debug_eprintln {
222+
() => ($crate::debug_eprintln!(""));
223+
($($arg:tt)*) => {
224+
#[cfg(debug_assertions)]
225+
eprintln!($($arg)*);
226+
#[cfg(not(debug_assertions))]
227+
$crate::consume_unused_variable!($($arg)*);
228+
};
229+
}

core/tauri/src/manager.rs

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use serialize_to_javascript::{default_template, DefaultTemplate, Template};
1616
use url::Url;
1717

1818
use tauri_macros::default_runtime;
19+
use tauri_utils::debug_eprintln;
1920
#[cfg(feature = "isolation")]
2021
use tauri_utils::pattern::isolation::RawIsolationPayload;
2122
use tauri_utils::{
@@ -95,8 +96,7 @@ fn set_csp<R: Runtime>(
9596
acc.style.push(hash.into());
9697
}
9798
_csp_hash => {
98-
#[cfg(debug_assertions)]
99-
eprintln!("Unknown CspHash variant encountered: {:?}", _csp_hash)
99+
debug_eprintln!("Unknown CspHash variant encountered: {:?}", _csp_hash);
100100
}
101101
}
102102

@@ -514,14 +514,12 @@ impl<R: Runtime> WindowManager<R> {
514514
.to_string();
515515

516516
if let Err(e) = SafePathBuf::new(path.clone().into()) {
517-
#[cfg(debug_assertions)]
518-
eprintln!("asset protocol path \"{}\" is not valid: {}", path, e);
517+
debug_eprintln!("asset protocol path \"{}\" is not valid: {}", path, e);
519518
return HttpResponseBuilder::new().status(403).body(Vec::new());
520519
}
521520

522521
if !asset_scope.is_allowed(&path) {
523-
#[cfg(debug_assertions)]
524-
eprintln!("asset protocol not configured to allow the path: {}", path);
522+
debug_eprintln!("asset protocol not configured to allow the path: {}", path);
525523
return HttpResponseBuilder::new().status(403).body(Vec::new());
526524
}
527525

@@ -543,17 +541,15 @@ impl<R: Runtime> WindowManager<R> {
543541
let mut file = match tokio::fs::File::open(path_.clone()).await {
544542
Ok(file) => file,
545543
Err(e) => {
546-
#[cfg(debug_assertions)]
547-
eprintln!("Failed to open asset: {}", e);
544+
debug_eprintln!("Failed to open asset: {}", e);
548545
return (headers, 404, buf);
549546
}
550547
};
551548
// Get the file size
552549
let file_size = match file.metadata().await {
553550
Ok(metadata) => metadata.len(),
554551
Err(e) => {
555-
#[cfg(debug_assertions)]
556-
eprintln!("Failed to read asset metadata: {}", e);
552+
debug_eprintln!("Failed to read asset metadata: {}", e);
557553
return (headers, 404, buf);
558554
}
559555
};
@@ -568,8 +564,7 @@ impl<R: Runtime> WindowManager<R> {
568564
) {
569565
Ok(r) => r,
570566
Err(e) => {
571-
#[cfg(debug_assertions)]
572-
eprintln!("Failed to parse range {}: {:?}", range, e);
567+
debug_eprintln!("Failed to parse range {}: {:?}", range, e);
573568
return (headers, 400, buf);
574569
}
575570
};
@@ -599,14 +594,12 @@ impl<R: Runtime> WindowManager<R> {
599594
);
600595

601596
if let Err(e) = file.seek(std::io::SeekFrom::Start(range.start)).await {
602-
#[cfg(debug_assertions)]
603-
eprintln!("Failed to seek file to {}: {}", range.start, e);
597+
debug_eprintln!("Failed to seek file to {}: {}", range.start, e);
604598
return (headers, 422, buf);
605599
}
606600

607601
if let Err(e) = file.take(real_length).read_to_end(&mut buf).await {
608-
#[cfg(debug_assertions)]
609-
eprintln!("Failed read file: {}", e);
602+
debug_eprintln!("Failed read file: {}", e);
610603
return (headers, 422, buf);
611604
}
612605
// partial content
@@ -631,8 +624,7 @@ impl<R: Runtime> WindowManager<R> {
631624
response.mimetype(&mime_type).body(data)
632625
}
633626
Err(e) => {
634-
#[cfg(debug_assertions)]
635-
eprintln!("Failed to read file: {}", e);
627+
debug_eprintln!("Failed to read file: {}", e);
636628
response.status(404).body(Vec::new())
637629
}
638630
}
@@ -756,19 +748,18 @@ impl<R: Runtime> WindowManager<R> {
756748
asset
757749
})
758750
.or_else(|| {
759-
#[cfg(debug_assertions)]
760-
eprintln!(
751+
debug_eprintln!(
761752
"Asset `{}` not found; fallback to {}/index.html",
762-
path, path
753+
path,
754+
path
763755
);
764756
let fallback = format!("{}/index.html", path.as_str()).into();
765757
let asset = assets.get(&fallback);
766758
asset_path = fallback;
767759
asset
768760
})
769761
.or_else(|| {
770-
#[cfg(debug_assertions)]
771-
eprintln!("Asset `{}` not found; fallback to index.html", path);
762+
debug_eprintln!("Asset `{}` not found; fallback to index.html", path);
772763
let fallback = AssetKey::from("index.html");
773764
let asset = assets.get(&fallback);
774765
asset_path = fallback;
@@ -806,8 +797,7 @@ impl<R: Runtime> WindowManager<R> {
806797
})
807798
}
808799
Err(e) => {
809-
#[cfg(debug_assertions)]
810-
eprintln!("{:?}", e); // TODO log::error!
800+
debug_eprintln!("{:?}", e); // TODO log::error!
811801
Err(Box::new(e))
812802
}
813803
}

0 commit comments

Comments
 (0)