Incorrect "Expression is always true" in consecutive LINQ Where() calls
Consider the following code:
public static void Example(string[] args) { foreach (string s in args .Where(arg => arg.Length > 10) // Essentially any check involving arg (or implying it's not null). .Select(Transform) // Transform returns null in some cases. .Where(s => s != null)) // s != null gives "Expression is always true" warning. { } } private static string Transform(string arg) { return arg.Length % 2 == 0 ? arg : null; }
Obviously enough, Transform() can return null, rendering the warning misleading (and possibly dangerous). In fact, this example shows a slightly realistic case, as just unconditionally returning null in Transform() has the same consequences.
If I remove the first Where() call, the warning goes away. So it does if I call a similar lambda instead of the Transform() method. Thus, seems like ReSharper doesn't look into the called methods when analysing the code, but why doesn't it even consider that a called method can return whatever it likes to?
Reproduced on VS 2013 with ReSharper 8.1.23.546.
Please sign in to leave a comment.