Version Used:
Visual Studio 2019 Preview 19.3, C# 7.3 (also tried 8.0 beta)
Steps to Reproduce:
using System;
namespace WrongRedundantCastWarning
{
struct Flag
{
public Flag(int value) => this.Value = value;
public int Value { get; }
// This cast is wrongly reported as redundant
public static FlagSet operator ~(Flag flag) => ~(FlagSet)flag;
}
struct FlagSet
{
public FlagSet(int value) => this.Value = value;
public int Value { get; }
public static implicit operator FlagSet(Flag flag) => new FlagSet(flag.Value);
public static FlagSet operator ~(FlagSet flagSet) => new FlagSet(~flagSet.Value);
}
class Program
{
static readonly Flag One = new Flag(1);
static readonly Flag Two = new Flag(2);
static void Main(string[] args)
{
var flipped = ~Two;
Console.WriteLine(flipped.Value);
}
}
}
The example has two types (Flag and FlagSet), with an implicit conversion defined from Flag to FlagSet.
Both types define the bitwise complement operator (~).
The implementation of Flag's bitwise complement operator invokes the implementation in FlagSet by casting itself to a FlagSet. This cast is not redundant -- without it, no conversion takes place, and the operator just calls itself leading to StackOverflowException.
Version Used:
Visual Studio 2019 Preview 19.3, C# 7.3 (also tried 8.0 beta)
Steps to Reproduce:
The example has two types (Flag and FlagSet), with an implicit conversion defined from Flag to FlagSet.
Both types define the bitwise complement operator (~).
The implementation of Flag's bitwise complement operator invokes the implementation in FlagSet by casting itself to a FlagSet. This cast is not redundant -- without it, no conversion takes place, and the operator just calls itself leading to StackOverflowException.