Modulo operator gives incorrect "Expression is always false" warning when used with a nullable int with a fallback value

I created a little console app with the following code:

using Playground;

Console.Write("Enter the 1st number: ");
var a = int.Parse(Console.ReadLine()!);

Console.Write("Enter the 2nd number: ");
var wrappedB = new ClassWithNullableInt { NullableInt = int.Parse(Console.ReadLine()!) };

if (a > 0) // Removing this line will get rid of the warning 
{
var b = wrappedB.NullableInt ?? 123; // Removing the fallback value will also get rid of the warning 
if (a % b == 0) // Warning: "Expression is always false"
Console.WriteLine("!");
}

Console.ReadLine();
namespace Playground;

public class ClassWithNullableInt
{
public int? NullableInt { get; set; }
}

If the 1st number is the same or a multiple of the 2nd number the console app will output "!".

It looks like Rider thinks that b must be a "0" for whatever reason. If you allow a to be negative (and remove the if) the error disappears.

0
1 comment

Hi Taukuno

Thank you for the report. I was able to reproduce the issue and created a corresponding bug in our tracker. Feel free to follow it for the updates! 

0

Please sign in to leave a comment.