Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StringComparison code fix provider generates invalid code #16

Open
bgrainger opened this issue Dec 31, 2018 · 0 comments
Open

StringComparison code fix provider generates invalid code #16

bgrainger opened this issue Dec 31, 2018 · 0 comments
Labels
bug Something isn't working

Comments

@bgrainger
Copy link
Member

The expression !a.Equals(b) is converted to !a == b, which is invalid C#. The suggested replacement should be a != b.

Test case to repro:

		[Test]
		public void DiagnoseAndFixNegatedStringEquals()
		{
			const string program = @"using System;
namespace ConsoleApplication1
{
	class Program
	{
		static void Main(string[] args)
		{
			GC.KeepAlive(!""a"".Equals(""b""));
		}
	}
}
";
			var expected = new DiagnosticResult
			{
				Id = OverloadWithStringComparisonAnalyzer.AvoidStringEqualsDiagnosticId,
				Message = "Use operator== or a non-ordinal StringComparison.",
				Severity = DiagnosticSeverity.Warning,
				Locations = new[] { new DiagnosticResultLocation("Test0.cs", 8, 22) },
			};

			VerifyCSharpDiagnostic(program, expected);

			const string fix1 = @"using System;
namespace ConsoleApplication1
{
	class Program
	{
		static void Main(string[] args)
		{
			GC.KeepAlive(""a"" != ""b"");
		}
	}
}
";
			VerifyCSharpFix(program, fix1, 0);

			const string fix2 = @"using System;
namespace ConsoleApplication1
{
	class Program
	{
		static void Main(string[] args)
		{
			GC.KeepAlive(!""a"".Equals(""b"", StringComparison.OrdinalIgnoreCase));
		}
	}
}
";
			VerifyCSharpFix(program, fix2, 1);
		}
@bgrainger bgrainger added the bug Something isn't working label Dec 31, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

No branches or pull requests

1 participant