使用了函数`print`,但为什么它警告我`unused`?

问题描述

这里的函数在于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);
}

如上所示,wasm 模块将仅使用 wasm32-unkNown-unkNown 目标进行编译。 huangjj27:env_loggersrc\fmt\writer\termcolor\shim_impl.rs 中使用了 print 函数

// 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!("{}",}

        Ok(())
    }

然后我用 node 测试它:

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

然后我得到了这个令人困惑的拒绝未使用问题:

[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,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"

我的问题是:

  1. 为什么会出现警告,而我确实在某处使用了函数 wasm::print
  2. 我该如何处理这个问题?解决或修复它是可以的(但我仍然需要启用 lint 配置)。

解决方法

如果你只在其他未使用的函数中使用某个函数,那么它仍然会给你警告。如果您想禁用该函数的警告,您可以通过将 #[allow(dead_code)] 放在函数标题之前的行上来实现。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...