C / C++ compiler warnings: do you clean up all your code to remove them or leave them in?

前端 未结 18 2190
夕颜
夕颜 2020-12-24 00:08

I\'ve worked on many projects where I\'ve been given code by others to update. More often than not I compile it and get about 1,000+ compiler warnings. When I see compiler w

相关标签:
18条回答
  • 2020-12-24 00:55

    The worst part is that when you write new code, its hard to know if you've accidentally introduced more warnings, since there are so many you just ignore them anyways.

    The problem with cleaning them all up, is that it takes time that you may or may not have. But yes, generally you should clean up as many as you can.

    0 讨论(0)
  • 2020-12-24 00:56

    I have worked with a number of embedded systems where the warnings will result in instability, crashing, or memory corruption. Unless you know the warning is innocuous, it should be dealt with.

    0 讨论(0)
  • 2020-12-24 00:56

    Always clean up all warnings or explicitly suppress them if needed. The default settings for warnings should be highest possible when compiling (level 4 on VS for example).

    0 讨论(0)
  • 2020-12-24 00:59

    Warnings are and should be treated as bugs. If you can't code well enough to get rid of your warnings then you probably shouldn't be coding. In my group we made the decision to force all warnings to errors. It ends this discussion altogether and really, IMHO, improves code quality.

    0 讨论(0)
  • 2020-12-24 01:01

    There are some instances where I will leave warnings in code, or where it's infeasible to clean them up (although I do remove the ones I can). For example:

    • If you have something you're working on, and you know it needs more work/attention, leaving a warning in place to indicate this can be appropriate
    • If you're compiling C++ with /clr, there are several warnings about things which cause native code to be generated; it can be cumbersome to suppress all these warnings when the codebase cannot be functionally changed
    • Cleaning up warnings when you don't understand what the fix does. I've done this a couple times with PC-Lint warning, and ended up introducing bugs. If you don't know what the exact effect of the change is (eg: C-style casts to eliminate warnings), DO NOT do it. Figure out the warning, or leave the code alone is my advice.

    Anyway, those are the instances off the top of my head where leaving warnings might be appropriate.

    0 讨论(0)
  • 2020-12-24 01:05

    I try to compile code with a fairly high level of warnings and clean them all up, except for "signed / unsigned comparison" warnings, which I'm sure I should fix but can never be bothered.

    Short version: In g++ I use "-Wextra -Wno-sign-compare" and get rid of all messages.

    0 讨论(0)
提交回复
热议问题