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

Fix S2259 FP: Recognize NotNull results of String methods #6158

Closed
pavel-mikula-sonarsource opened this issue Oct 3, 2022 · 2 comments · Fixed by #7838
Closed

Fix S2259 FP: Recognize NotNull results of String methods #6158

pavel-mikula-sonarsource opened this issue Oct 3, 2022 · 2 comments · Fixed by #7838
Assignees
Labels
Area: C# C# rules related issues. Area: CFG/SE CFG and SE related issues. Area: VB.NET VB.NET rules related issues. Sprint: SE Short-lived* label for epic MMF-3077 *troll Type: CFG/SE FPs Rule IS triggered when it shouldn't be for CFG and SE rules.
Milestone

Comments

@pavel-mikula-sonarsource
Copy link
Contributor

pavel-mikula-sonarsource commented Oct 3, 2022

Methods like str.Trim() never return null. We should learn that from their invocations. It's similar to what we do for Linq Enumerable.

We need to go through the methods and identify those that never return null like Trim, SubString, and so on

It will fix this issue from Peach: https://peach.sonarsource.com/project/issues?issues=AYORbz8FkNA1ahe47Aev&open=AYORbz8FkNA1ahe47Aev&id=dotnet-runtime

        internal static Grammar Create(string grammarName, string ruleName)
        {
            grammarName = grammarName.Trim();
            Uri uriGrammar;
            bool hasUri = Uri.TryCreate(grammarName, UriKind.Absolute, out uriGrammar); // Is annotated with NotNullWhenAttribute, we learn Null and NotNull cases
            int posDll = grammarName.IndexOf(".dll", StringComparison.OrdinalIgnoreCase); // FP
@pavel-mikula-sonarsource pavel-mikula-sonarsource added Type: Improvement Making existing code better. Area: CFG/SE CFG and SE related issues. Area: VB.NET VB.NET rules related issues. Area: C# C# rules related issues. labels Oct 3, 2022
@pavel-mikula-sonarsource pavel-mikula-sonarsource changed the title Improve S2259: Recognize NotNull results of String methods Fix S2259 FP: Recognize NotNull results of String methods Oct 3, 2022
@github-actions github-actions bot added this to Improvement in Backlog Oct 3, 2022
@martin-strecker-sonarsource
Copy link
Contributor

According to https://raw.githubusercontent.com/dotnet/runtime/main/src/libraries/System.Runtime/ref/System.Runtime.cs there is only one method that may return null: string.IsInterned. It seems that whitelisting "IsInterned" is the easiest way for string.

@pavel-mikula-sonarsource pavel-mikula-sonarsource added Type: CFG/SE FPs Rule IS triggered when it shouldn't be for CFG and SE rules. and removed Type: Improvement Making existing code better. labels Aug 18, 2023
@mary-georgiou-sonarsource mary-georgiou-sonarsource added this to In progress in Best Kanban Aug 18, 2023
@mary-georgiou-sonarsource mary-georgiou-sonarsource added this to the 9.9 milestone Aug 18, 2023
@mary-georgiou-sonarsource mary-georgiou-sonarsource added the Sprint: SE Short-lived* label for epic MMF-3077 *troll label Aug 21, 2023
@mary-georgiou-sonarsource
Copy link
Contributor

According to https://raw.githubusercontent.com/dotnet/runtime/main/src/libraries/System.Runtime/ref/System.Runtime.cs there is only one method that may return null: string.IsInterned. It seems that whitelisting "IsInterned" is the easiest way for string.

Maybe it's better to actually list the methods instead of just exclude one because in case a new String method is added that returns null we'll have an error.

@github-actions github-actions bot moved this from In progress to Review in progress in Best Kanban Aug 22, 2023
@github-actions github-actions bot moved this from Review in progress to In progress in Best Kanban Aug 22, 2023
@github-actions github-actions bot moved this from In progress to Review in progress in Best Kanban Aug 22, 2023
@github-actions github-actions bot moved this from Review in progress to In progress in Best Kanban Aug 23, 2023
@github-actions github-actions bot moved this from In progress to Review in progress in Best Kanban Aug 24, 2023
@github-actions github-actions bot moved this from Review in progress to Review approved in Best Kanban Aug 24, 2023
Best Kanban automation moved this from Review approved to Validate Peach Aug 24, 2023
@Tim-Pohlmann Tim-Pohlmann moved this from Validate Peach to Done in Best Kanban Aug 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: C# C# rules related issues. Area: CFG/SE CFG and SE related issues. Area: VB.NET VB.NET rules related issues. Sprint: SE Short-lived* label for epic MMF-3077 *troll Type: CFG/SE FPs Rule IS triggered when it shouldn't be for CFG and SE rules.
Projects
Best Kanban
  
Done
Backlog
  
Improvement
Development

Successfully merging a pull request may close this issue.

4 participants