dotnet/roslyn

Clarify diagnostic when constant pattern has non-constant conversion to input type

Geschlossen

#63.476 geöffnet am 18.08.2022

 (1 Kommentar) (0 Reaktionen) (0 zugewiesene Personen)C# (4.257 Forks)batch import
Area-CompilersBugConcept-Diagnostic Clarityhelp wanted

Repository-Metriken

Stars
 (20.414 Sterne)
PR-Merge-Metriken
 (Durchschn. Merge 6T 17h) (256 gemergte PRs in 30 T)

Beschreibung

The error given for the below scenario could be made more clear: it is not the string literal itself which is the problem, it is the conversion to the switch input type which makes it a problem. SharpLab.

Discussed in https://github.com/dotnet/roslyn/discussions/63400

Originally posted by zms9110750 August 15, 2022

foreach (var ele in XElement.Load("D:\\temp.xml").Nodes())
{
	switch (ele)
	{
		case XElement xel:
			const string br = "br";
			Console.WriteLine(xel.Name switch
			{
				"br" => "\n", //CS0150
				br => "\n",	  //CS0150
				S.br => "\n", //CS0150
			});
			break;

		case XText txt:
			Console.WriteLine(txt);
			break;
	}
}
static class S
{
	public const string br = "br";
}

Why does pattern matching not treat strings as constants

Contributor Guide