-
Notifications
You must be signed in to change notification settings - Fork 231
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
Conversation
@@ -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> |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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 || |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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);
Fix #461