function `print` is used but why it warn me `unused`?

Viewed 208

Here is the function lies in huangjj27:env_logger/src/writer/wasm.rs

//! logging functions from wasm-bindgen.
//!
//! Here use the one-param logging functions, all messages should be transformed
//! to string before passing to the functions. Note that we only need this
//! module for `wasm32-unknown-unknown` target
#![cfg(all(target_arch = "wasm32", target_vendor = "unknown"))]

// use log::Level;
use wasm_bindgen::prelude::*;

use crate::fmt::glob::Target;

pub(in crate::fmt::writer) fn print(msg: &str, t: Target) {
    // work around for unused variable
    let _ = t;

    log(&msg);
}

As is shown above, the wasm module will only compile with wasm32-unknown-unknown target. And the print function is used in huangjj27:env_loggersrc\fmt\writer\termcolor\shim_impl.rs:

// huangjj27:env_loggersrc\fmt\writer\termcolor\shim_impl.rs: 32-48

    pub(in crate::fmt::writer) fn print(&self, buf: &Buffer) -> io::Result<()> {
        // This impl uses the `eprint` and `print` macros
        // instead of using the streams directly.
        // This is so their output can be captured by `cargo test`
        let log = String::from_utf8_lossy(&buf.0);

        #[cfg(all(target_arch = "wasm32", target_vendor = "unknown"))]
        wasm::print(&log, self.target);

        #[cfg(not(all(target_arch = "wasm32", target_vendor = "unknown")))]
        match self.target {
            Target::Stderr => eprint!("{}", log),
            Target::Stdout => print!("{}", log),
        }

        Ok(())
    }

then I test it with the node:

wasm-pack test --node -- --no-default-features --test node

then I get this confusing denied unused issue:

[INFO]: Checking for the Wasm target...
   Compiling env_logger v0.8.2 (C:\Users\huangjj27\Documents\codes\env_logger)
error: function is never used: `print`
   --> src\fmt\writer\wasm.rs:13:31
    |
13  | pub(in crate::fmt::writer) fn print(msg: &str, t: Target) {
    |                               ^^^^^
    |
note: lint level defined here
   --> src\lib.rs:280:54
    |
280 | #![deny(missing_debug_implementations, missing_docs, warnings)]
    |                                                      ^^^^^^^^
    = note: `#[deny(dead_code)]` implied by `#[deny(warnings)]`

error: function is never used: `print`
   --> src\fmt\writer\wasm.rs:13:31
    |
13  | pub(in crate::fmt::writer) fn print(msg: &str, t: Target) {
    |                               ^^^^^
    |
note: lint level defined here
   --> src\lib.rs:280:54
    |
280 | #![deny(missing_debug_implementations, missing_docs, warnings)]
    |                                                      ^^^^^^^^
    = note: `#[deny(dead_code)]` implied by `#[deny(warnings)]`

error: aborting due to previous error

error: could not compile `env_logger`.
warning: build failed, waiting for other jobs to finish...
error: aborting due to previous error

error: could not compile `env_logger`.

To learn more, run the command again with --verbose.
Error: Compilation of your program failed
Caused by: failed to execute `cargo build`: exited with exit code: 101
  full command: "cargo" "build" "--tests" "--target" "wasm32-unknown-unknown"

My questions are:

  1. Why does the warning come out, while I indeed use the function wasm::print somewhere?
  2. How could I deal with this problem? Working around or fixing it is ok (but I still need to keep the lint config enabled).
1 Answers

If you only use a function in other unused functions, then it will still give you the warning. If you want to disable the warning on that one function, you can do that by putting #[allow(dead_code)] on the line before the function header.

Related