C# Compiler should give warning but doesn't?

后端 未结 7 1140
北恋
北恋 2020-12-03 14:05

Someone on my team tried fixing a \'variable not used\' warning in an empty catch clause.

try { ... } catch (Exception ex) { }

-> gives a w

7条回答
  •  渐次进展
    2020-12-03 14:56

    It's not really the job of a compiler to work out every single instance and corner case when a variable may or may not be used. Some are easy to spot, some are more problematic. Erring on the side of caution is the sensible thing to do (especially when warnings can be set to be treated as errors - imagine if software didn't compile just because the compiler thought you weren't using something you were). Microsoft Compiler team specifically say:

    "...our guidance for customers who are interested in discovering unused elements in their code is to use FxCop. It can discover unused fields and much more interesting data about your code."

    -Ed Maurer, Development Lead, Managed Compiler Platform

提交回复
热议问题