Live data from Hacker News

GCC 6: -Wmisleading-indentation vs. “goto fail;”

developerblog.redhat.com

11–20 of 168 posts

Re: GCC 6: -Wmisleading-indentation vs. “goto fail;”

#12
post #7

Earlier quoted context omitted.

This warning is a practical solution to a real problem (of which the famous "goto fail" is an example). Using braces everywhere is definitely good style, but it isn't a practical solution because there's a ton of existing C/C++ code that doesn't use braces.

Wouldn't it make sense to update that as you change the code? If you edit an if statement, you should add the braces in. It would make sure that no new errors are introduced due to this.

That's a good policy for code reviews, but again it's hard to do it correctly in a tool.

The compiler would need to require braces in new code, but allow brace-free style in old code; you'd need to integrate it with the source control system so it knows what code is new. Doable, but tricky.

Re: GCC 6: -Wmisleading-indentation vs. “goto fail;”

#13
Making braces optional in single-statement if/else/while/for clauses is one of the biggest anti-features in C. It's frustrating that it was ported forward to more modern languages like Java, JavaScript, C#, etc.

I'm glad Python (with semantic whitespace) and Go (with gofmt) solve this problem.

Re: GCC 6: -Wmisleading-indentation vs. “goto fail;”

#14
post #7

Earlier quoted context omitted.

This warning is a practical solution to a real problem (of which the famous "goto fail" is an example). Using braces everywhere is definitely good style, but it isn't a practical solution because there's a ton of existing C/C++ code that doesn't use braces.

Wouldn't it make sense to update that as you change the code? If you edit an if statement, you should add the braces in. It would make sure that no new errors are introduced due to this.

Of course, that's the (silent) assumption. But humans sometimes fail to do this, which is why the warning is helpful. Very nice to see this in GCC.

Re: GCC 6: -Wmisleading-indentation vs. “goto fail;”

#15
post #2

This seems like it could have been avoided by people using braces around every block. Omitting braces in this case leads to a lot of problems.

I'm in favor of braces around everything, but to be honest even I will occasionally use the indent if I only expect one action. e.g. if (true) foo(); else bar(); This looks prettier to my eyes than if(true){ foo(); } else { bar(); } However, I might not be the last person to touch the code. My coworker might come later and add: if (true) foo(); else bar(); baz(); And hence the indent problem.

Btw, I suppose you wanted to add baz() after bar(). Adding it after foo(), as you did, will not compile.

Re: GCC 6: -Wmisleading-indentation vs. “goto fail;”

#16
post #2

This seems like it could have been avoided by people using braces around every block. Omitting braces in this case leads to a lot of problems.

I'm in favor of braces around everything, but to be honest even I will occasionally use the indent if I only expect one action. e.g. if (true) foo(); else bar(); This looks prettier to my eyes than if(true){ foo(); } else { bar(); } However, I might not be the last person to touch the code. My coworker might come later and add: if (true) foo(); else bar(); baz(); And hence the indent problem.

Prettier but potentially dangerous? That's the core of the problem, I believe.

Re: GCC 6: -Wmisleading-indentation vs. “goto fail;”

#18

What a great idea. It's hard to believe no-one ever did this before! (Or did they?)

Well, some IDEs will warn you even during editing - a step ahead of compilation (e.g. IDEA and its kin). The earlier this antipattern is caught, the easier it is to fix.

Re: GCC 6: -Wmisleading-indentation vs. “goto fail;”

#19

Making braces optional in single-statement if/else/while/for clauses is one of the biggest anti-features in C. It's frustrating that it was ported forward to more modern languages like Java, JavaScript, C#, etc. I'm glad Python (with semantic whitespace) and Go (with gofmt) solve this problem.

In Rust, code like this simply won't compile http://is.gd/e6mBlG

Re: GCC 6: -Wmisleading-indentation vs. “goto fail;”

#20

Making braces optional in single-statement if/else/while/for clauses is one of the biggest anti-features in C. It's frustrating that it was ported forward to more modern languages like Java, JavaScript, C#, etc. I'm glad Python (with semantic whitespace) and Go (with gofmt) solve this problem.

Single-line ifs are pretty useful with traditional-style C libraries that expect all checks to be done at call site:

  if (ptr) call_oldschool_thingy(ptr);
Post reply on HN