如何让Xcode 3.1编译器在if语句中警告赋值运算符? [英] How to have the Xcode 3.1 compiler warn of assignment operator in an if statement?

查看:208
本文介绍了如何让Xcode 3.1编译器在if语句中警告赋值运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试尽可能地搜索文档和互联网,但是如果在if语句中使用赋值运算符,我无法让Xcode编译器发出警告。 p>

我来自RealBasic,在那里我有一个非常强的习惯键入这种比较:

 如果x = 5则... 

当然,语法分配x的值5然后测试结果以查看它是否为非零,并且正确运算符是:

  if(x == 5){...} 

编译器应该能够警告如果有一个赋值在一个if比较,但我不能找到如何打开它在Xcode 3.1 / gcc。我发现 -pedantic 选项,但是似乎没有产生警告。



花费了大量的时间两次,现在跟踪bug,结果是=而不是==,我想要一个警告的帮助。



我知道我可以这样做(这将导致编译器错误):

  if(5 = x){。 ..} 

...但这需要改变根深蒂固的编码习惯。



谢谢!

解决方案

p>在ennuikiller的回答中的链接导致了我需要的。



这里有一些更多的信息,可能会在将来找到这个:




  • 编译器警告是缺少括号和括号或-Wparentheses。


  • p>通过在Xcode中为当前
    目标选择获取信息来打开警告。


  • 正如我终于发现的,iPhone模拟器SDK


  • 将SDK切换到iPhone设备会显示GCC 4.2警告
    部分,其中包括缺少
    大括号和括号。


  • 在iPhone设备SDK中打开该选项后, $ b给Similator显示
    User-Defined构建设置已添加

    GCC_WARN_MISSING_PARENTHESES =
    />


  • 如果在if语句中确实需要赋值,可以
    加上括号以避免警告。




(双括号示例)

  if((x = 5)){...} 

像一个魅力,虽然它不喜欢苹果的标准:

  if(self = [super init]){...} 

我可能会保留原样,虽然如果我结束了大量的一个项目,我将双括号或分割成一个单独的行。



谢谢!


I've tried searching the documentation and the internet as best as I'm able, but I haven't been able to get the Xcode compiler to issue a warning if the assignment operator is used in an if statement.

I'm coming from RealBasic, where I've got an extremely strong habit of typing this sort of comparison:

if x = 5 then ...

In C, of course, that syntax assigns x the value of 5 then tests the result to see if it's nonzero, and the "correct" operator is:

if (x == 5) { ... }

I've found several mentions that the compiler should be able to warn if there is an assignment made in an if comparison, but I cannot find how to turn it on in Xcode 3.1/gcc. I found the -pedantic option, but that didn't seem to generate the warning.

Since I've spent a fair amount of time twice now tracking down bugs that turned out to be "=" instead of "==", I'd like the help of a warning.

I know that I can do this instead (which will cause a compiler error):

if (5 = x) { ... }

...but that requires changing ingrained coding habits, too. Not to mention that it looks clumsy and backward.

Thanks!

解决方案

The link in ennuikiller's answer did lead to what I needed.

Here's a bit more information for anyone who might find this in the future:

  • The compiler warning is "Missing braces and parentheses", or -Wparentheses.

  • The warning is turned on by choosing Get Info for the current target in Xcode.

  • As I finally found out, the iPhone Simulator SDK does not show this compiler option.

  • Switching the SDK to iPhone Device will show the GCC 4.2 Warnings section, which includes "Missing braces and parentheses".

  • After turning on that option in the iPhone Device SDK, switching back to the Similator shows that a "User-Defined" build setting has been added: GCC_WARN_MISSING_PARENTHESES = "YES".

  • If an assignment is really desired in an if statement, you can double the parentheses to avoid the warning.

(Double-parentheses example:)

if ((x = 5)) { ... }

The warning works like a charm, though it does dislike Apple's standard:

if (self = [super init]) { ... }

I'll probably leave those as-is, though if I end up with a huge amount of them in a project, I'll either double the parenthesis or break out the assignment into a separate line.

Thanks!

这篇关于如何让Xcode 3.1编译器在if语句中警告赋值运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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