将*一些*警告视为Swift中的错误? [英] Treat *some* warnings as errors in Swift?

查看:38
本文介绍了将*一些*警告视为Swift中的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下,我在 Swift 中标记以下方法已弃用:

Imagine I mark the following method deprecated in Swift:

@available(*, deprecated=1.0)
func myFunc() { 
    // ...
}

并且我通过设置 OTHER_SWIFT_FLAGS="将警告视为 Swift 中的错误-warnings-as-errors".

And I treat warnings as errors in Swift by setting OTHER_SWIFT_FLAGS="-warnings-as-errors".

如何让它将这些弃用通知显示为警告,同时仍将其余警告视为错误?

How do I make it show these deprecation notices as warnings, while still treating the rest of the warnings as errors?

似乎GCC 对这个问题有一个很好的解决方案:

-Werror // treat all warnings as errors
-Wno-error=<warning> // don't treat <warning> as error (e.g. -Wno-error=switch)
-Werror=<warning> // treat <warning> as error

所以如果这是 Objective-C,我可以简单地使用 -Werror -Wno-error=deprecated-declarations 并得到我想要的.

So if this was Objective-C, I could simply use -Werror -Wno-error=deprecated-declarations and get exactly what I want.

Swift 的等价物是什么?

What is the equivalent for Swift?

我尝试将 -Wno-error=deprecated-declarations 添加到 OTHER_SWIFT_FLAGS,但它似乎不适用于 Swift,所以它不起作用.

I tried adding -Wno-error=deprecated-declarations to the OTHER_SWIFT_FLAGS, but it seems like it's not meant for Swift, so it doesn't work.

推荐答案

这不可能.从 Swift 4 开始,Swift 编译器没有用于启用/禁用特定警告或将特定警告升级为错误的开关.

This isn't possible. As of Swift 4, the Swift compiler doesn't have switches to either enable/disable particular warnings or promote particular warnings to errors.

Swift 核心开发人员多次表示不愿在 swift-evolution 邮件列表中添加一连串编译器标志.其基本原理是他们希望保留单一的方言".Swift 以便每个开发人员使用相同的语言功能等.

The Swift core developers expressed their reluctance to add a litany of compiler flags on several occasions on the swift-evolution mailing list. The rationale is that they want to keep a single "dialect" of Swift so that every developer works with the same language features etc.

这是否应该扩展到特定警告标志之类的东西当然是有争议的,但这是目前的官方立场.未来这些规则肯定有可能会有所放松,但我不会打赌.

Whether this should extend to something like particular warning flags is of course debatable, but that's the current official stance. It's definitely possible that these rules will be somewhat loosened in the future, but I wouldn't bet on it.

从 Swift 4.2 开始,Swift 添加了 #warning 语法.

As of Swift 4.2, Swift added a #warning syntax.

这篇关于将*一些*警告视为Swift中的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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