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

New rule S1133: Deprecated code should be removed #6669

Merged
merged 21 commits into from Feb 2, 2023

Conversation

martin-strecker-sonarsource
Copy link
Contributor

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just added a couple of comments for more testcases


protected override string MessageFormat => "Do not forget to remove this deprecated code someday.";

protected RemoveObsoleteCodeBase() : base(DiagnosticId) { }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a big overlap to ObsoleteAttributesNeedExplanation:

protected ObsoleteAttributesNeedExplanationBase() : base(DiagnosticId) { }
protected sealed override void Initialize(SonarAnalysisContext context) =>
context.RegisterNodeAction(
Language.GeneratedCodeRecognizer,
c =>
{
if (c.SemanticModel.GetSymbolInfo(c.Node).Symbol is { } attribute
&& attribute.IsInType(KnownType.System_ObsoleteAttribute)
&& !attribute.GetParameters().Any())
{
c.ReportIssue(Diagnostic.Create(Rule, c.Node.GetLocation()));
}
},
Language.SyntaxKind.Attribute);

What should we do @andrei-epure-sonarsource

  • Merge this PR into master and do follow-up to merge the two rules
  • Merge the two rules as part of this PR

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you mean merging the implementation of the rules, I'd suggest to do it in a separate PR

if you mean to merge the rules themselves, it should be a discussion at LT level as these are already implemented in other languages, too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant merging the implementations. I created #6698 to track the work.

@Corniel
Copy link
Contributor

Corniel commented Feb 1, 2023

What about the following case?

[Obsolete("Will be dropped when the next major version is released.")]
public class Will_be_dropped
{
    [Test]
    public void TestSomething() => //..
}

Tests that are marked as obsolete are in most (if not all) cases decorated as such because the call deprecated code, as a way to prevent CS0618. Do we want to ignore those?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, really extensive tests!

@andrei-epure-sonarsource
Copy link
Contributor

(please don't wait for a review from me to merge it, I don't have capacity to review it all and don't want to block it ; I was just wandering around the PRs previously when I left the comment)

@martin-strecker-sonarsource
Copy link
Contributor Author

Tests that are marked as obsolete are in most (if not all) cases decorated as such because the call deprecated code, as a way to prevent CS0618. Do we want to ignore those?

I don't think we want to make any exceptions to the rule. The rule is to enable users in SonarCloud/SonarQube to track obsolete members. CS0618 doesn't raise when obsolete code calls obsolete code. The equivalent for this rule would be obsolete code scoped within obsolete code (e.g. obsolete method inside an obsolete class). I would still raise at both declarations because they might have a different deprecation policy.

The scope for the rule is "All" and this includes tests. Tests that call obsolete code will be removed along with the obsolete member itself. So I don't see why we shouldn't track the obsolete test class.

@sonarcloud
Copy link

sonarcloud bot commented Feb 1, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@sonarcloud
Copy link

sonarcloud bot commented Feb 1, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

100.0% 100.0% Coverage
0.0% 0.0% Duplication

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, amazing testcase coverage.

Overly nitpick suggestion, feel free to ignore it and merge, I already approved:

I saw you added a record example for versions >= C#10, maybe add these as well:

 [Obsolete]
record class RecordClass { }

[Obsolete]
record struct RecordStruct { }

[Obsolete]
ref struct RefStruct // still no idea what this does
{
    [Obsolete]
    public RefStruct()    {    }

    [Obsolete]
    ref int MeaningOfLife = 42; // C# 11 weirdness
}

@martin-strecker-sonarsource
Copy link
Contributor Author

@gregory-paidis-sonarsource This doesn't add much value with respect to the current implementation. When I did c.ContaingSymbol it was important to test every AttributeTarget to make sure c.ContaingSymbol points to the right symbol (which as it turned out doesn't always do). Now we start with the [Obsolete] declaration itself and we don't care where it is actually applied to.

@Corniel
Copy link
Contributor

Corniel commented Feb 1, 2023

The scope for the rule is "All" and this includes tests. Tests that call obsolete code will be removed along with the obsolete member itself. So I don't see why we shouldn't track the obsolete test class.

Because you will have two warnings (S1133 and CS0618) if you have one test on your obsolete code, which is noise. You do not have the risk of not removing the test, as by removing the obsolete test, your test will not longer compile.

More generic, you could argue that a method that is marked as [Obsolete] that contains any code that is obsolete itself should not be triggered. Although I would not go down that road. That being said, having this rules on unit test methods or classes seems like FP's to me.

@martin-strecker-sonarsource martin-strecker-sonarsource merged commit 68b93a2 into master Feb 2, 2023
@martin-strecker-sonarsource martin-strecker-sonarsource deleted the Martin/S1133_WarnOnObsolete branch February 2, 2023 12:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New rule S1133: Deprecated code should be removed
5 participants