views:

219

answers:

2

I'm using ReSharper 5.0, and am wondering how its code analysis function knows to higlight the following assemblies == null with the comment "Expression is always false".

var directory = new DirectoryInfo("somedir");
FileInfo[] assemblies = directory.GetFiles("*.dll");

if (assemblies == null <<--- this is highlighted with "Expression is always false"
    || assemblies.Length == 0)
{
  _log.Warn("No assemblies found");
}

I'd understand if the return type was a value-type, which it isn't. I'd also understand if there was some sort of code contract or metadata stating .GetFiles() will never return null. but I don't think there is.

So - how does it know this? Am I missing something obvious, or does ReSharper have some privileged knowledge, such as an internal list of metadata about framework methods? Or does it actually "introspect" the internal code and work it out?

+15  A: 

The ReSharper developers ran flow analysis on the .NET framework binaries and determined which methods may or may not return null. Apparently DirectoryInfo.GetFiles never returns null.

You can annotate your own code to indicate the same set of rules, with a set of JetBrains. attributes. Take a look at the ReSharper site: http://www.jetbrains.com/resharper/features/code_analysis.html#Annotated_Framework

Edit: to answer your question specifically, "does ReSharper have some privileged knowledge, such as an internal list of metadata about framework methods" - yes, it came from "introspecting the internal code and working it out"

Tim Robinson
@Tim - makes sense - thanks for the reply.
Rob Levine
The "warning" seems to suggest that you remove the `assemblies == null` expression. However, if it was generated by looking at implemented code, that isn't necessarily a documented code contract that I would want to rely on.
It's up to you; I've double-checked Resharper's warnings enough times in the past that I trust it now. Besides, you can never guarantee that all reference types will be non-null all of the time, so rather than put `== null` checks everywhere, I leave them out and make sure the code fails as early as possible.
Tim Robinson
+2  A: 

As Tim points out, we annotate the .NET Framework. It's similar to what you get with Code Contracts, but done a little bit differently. If you look under the bin folder in ReSharper installation, you can see all the annotations.

Hadi Hariri
@Hadi: Please refrain from using a signature block. Your gravatar and a link to your profile are already attached to every post. You're free to add whatever additional information you want to your profile.
Bill the Lizard
I'm curious: how did you gather the information for those annotations? Did you do static analysis of the BCL binaries, as the accepted answer suggests? Or did you go about it some other way?
Joe White
@Bill - I mostly agree with your point about sigs. However, in this one case, it underlines the authority with which Hadi speaks, since he works for JetBrains. Possibly an extra line to say it in plain English may have been more appropriate, but the sig did add to his answer.
Rob Levine
@Rob: An extra line in plain English would be appropriate in this case.
Bill the Lizard
@Bill the Lizard - fair enough
Rob Levine
@Bill, sorry. I'm actually new to this whole world of Stackoverflow.
Hadi Hariri
@Joe, we have a tool that analyses the BCL and we're considering maybe making it public at some point for others to use.
Hadi Hariri
@Hadi Hariri - I'd sign up for that - I look forward to yet another JetBrains tool :)
Rob Levine