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

Rule S4035: Classes implementing "IEquatable<T>" should be sealed #463

Merged
merged 5 commits into from
Jun 22, 2017

Conversation

Evangelink
Copy link
Contributor

Fix #461

@Evangelink Evangelink requested a review from michalb-sonar June 22, 2017 10:14
@@ -2233,7 +2233,7 @@
<value>misra,cert</value>
</data>
<data name="S2201_Title" xml:space="preserve">
<value>Return values from functions without side effects should not be ignored</value>
<value>Return values should not be ignored when function calls don't have any side effects</value>
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove from this change, or move to a separate commit.

namedTypeSymbol.TypeArguments.Length == 1;
}

private static bool IsValidEqualsMethodSymbol(IMethodSymbol methodSymbol)
Copy link
Contributor

Choose a reason for hiding this comment

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

It's not clear what exactly "valid" means. Perhaps rename the method to make it more obvious?

foreach (var iequatable in equatableInterfacesByTypeName)
{
var associatedMethod = equalsMethodsByTypeName.GetValueOrDefault(iequatable.Key);
if (associatedMethod == null ||
Copy link
Contributor

Choose a reason for hiding this comment

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

If both conditions are short, I'd move them to one line.

It's a minor style comment, feel free to ignore.

methodSymbol.Parameters.Length == 1;
}

private static bool HasInvalidCombination(INamedTypeSymbol classSymbol)
Copy link
Contributor

Choose a reason for hiding this comment

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

Similar to above - could the method name give a hint on what invalid combination is?

var equatableInterfacesByTypeName = classSymbol.AllInterfaces
.Where(IsValidEquatableInterfaceSymbol)
.ToDictionary(nts => nts.TypeArguments[0].Name, nts => nts);
var equalsMethodsByTypeName = classSymbol.GetMembers(EqualsMethodName)
Copy link
Contributor

Choose a reason for hiding this comment

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

New line here would make it more readable IMHO.


// For all Equals(T) not a IEquatable<T> implementation checks if any is non-virtual
var unprocessedTypeNames = equalsMethodsByTypeName.Keys.Except(equatableInterfacesByTypeName.Keys);
foreach (var typeName in unprocessedTypeNames)
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps rewrite with linq?

return unprocessedTypeNames.Any(typeName => equalsMethodsByTypeName[typeName].IsVirtual);

@Evangelink Evangelink merged commit ae32956 into master Jun 22, 2017
@Evangelink Evangelink deleted the rule-s4035 branch June 22, 2017 12:40
mary-georgiou-sonarsource added a commit that referenced this pull request Nov 28, 2024
…ad lock has been acquired and vice versa for VB.NET (#463)
hashicorp-vault-sonar-prod bot pushed a commit that referenced this pull request Nov 29, 2024
…ad lock has been acquired and vice versa for VB.NET (#463)
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.

2 participants