2

这是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-unknownprint函数在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(())
    }

然后我用节点测试它:

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

我的问题是:

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

1 回答 1

0

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

于 2021-01-25T17:34:27.047 回答