dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.34k stars 4.74k forks source link

Analyzer: Validate literal arguments to StringSyntaxAttribute parameters/members #64009

Open stephentoub opened 2 years ago

stephentoub commented 2 years ago

https://github.com/dotnet/runtime/pull/62995 added the new StringSyntaxAttribute, and we annotated a bunch of APIs with it, for the three syntaxes currently identified: Regex, DateTimeFormat, and Json.

Visual Studio will look for these and use it to influence its colorization, IntelliSense, etc., but we can also use them to help warn developers at dev time / build time when they've written something that will produce run-time failures. We can add an analyzer for each syntax that will warn on the literal provided, e.g. these would warn

var r = new Regex("[abc"); //  Invalid pattern '[abc' at offset 4. Unterminated [] set.
DateTime.ParseExact(DateTime.Now.ToString(), "c", null); // Invalid format specifier
dotnet-issue-labeler[bot] commented 2 years ago

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

ghost commented 2 years ago

Tagging subscribers to this area: @dotnet/area-system-runtime See info in area-owners.md if you want to be subscribed.

Issue Details
https://github.com/dotnet/runtime/pull/62995 adds the new StringSyntaxAttribute and annotates a bunch of APIs with it, for the two syntaxes currently identified: Regex and DateTimeFormat. Visual Studio will look for these and use it to influence its colorization, IntelliSense, etc., but we can also use them to help warn developers at dev time / build time when they've written something that will produce run-time failures. We can add an analyzer for each syntax that will warn on the literal provided, e.g. these would warn ```C# var r = new Regex("[abc"); // Invalid pattern '[abc' at offset 4. Unterminated [] set. DateTime.ParseExact(DateTime.Now.ToString(), "c", null); // Invalid format specifier ```
Author: stephentoub
Assignees: -
Labels: `area-System.Runtime`, `untriaged`, `code-analyzer`
Milestone: -