在编译时发出警告? [英] Issuing a warning at compile time?

查看:89
本文介绍了在编译时发出警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在编译时发出警告,也许是从宏发出的。 cap_lints 不能使其静音。我当前的用例是功能弃用,但是还有其他可能的用途。

I want to issue a warning at compile time, perhaps from a macro. It should not be silenceable by cap_lints. My current use case is feature deprecation, but there's other possible uses for this.

推荐答案

目前在稳定的Rust中无法实现。但是,有一个不稳定的功能过程宏诊断程序,该功能可为通过 Diagnostic API。

This currently isn't possible in stable Rust. However, there is an unstable feature, procedural macro diagnostics, which provides this functionality for procedural macros, via the Diagnostic API.

要从程序宏内部发出编译器警告,请按以下方式使用它:

To emit a compiler warning from inside a procedural macro, you would use it like this:

#![feature(proc_macro_diagnostic)]

use proc_macro::Diagnostic;

Diagnostic::new()
    .warning("This method is deprecated")
    .emit();

要将警告与特定令牌范围关联,请使用 spanned_warning 。这会使警告输出在消息旁边显示带下划线的相关源标记。

To associate the warning with a specific token span, you'd use spanned_warning instead. This makes the warning output show the relevant source tokens underlined along with the message.

这篇关于在编译时发出警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆