-
Notifications
You must be signed in to change notification settings - Fork 468
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
Messaging for RS1024 (Compare symbols correctly) is unclear #3427
Comments
This is the expected behavior. When building against a version of roslyn that provides |
@sharwell could you explain a bit more please? The warning message says the symbols are not compared correctly, from my tests I think the comparison is correct. If there's another reason to refactor my code, at least the message should be changed a bit? |
|
I can mute the warning, so it's not a big issue. As a user, I just don't understand why the IDE wants me to write |
Because the above could be a bug, and the user intent isn't clear in the code. i.e. you may have purposefully left out the equality comaprer, or you may have mistakingly done it. The point of the error is to push users to be explicit so that intent is clear in the code. i.e. if you write:
|
@CyrusNajmabadi I do not agree with you. The analyzer 100% knows that I'm calling As it stands:
The intent is very clear, I'm comparing two symbols for equality. If you find the meaning of "equality" ambiguous then you should remove operators overloading Nothing in your argument is specific to symbols, so taking this to its logical conclusion, we should have a general analyzer that forbids all those things on all objects. Anyway, if you think there's something special and magical about symbols; I can always silent this warning, so no biggie. |
@jods4 The team who created this API incorrectly compared symbols in a number of cases that led to subtle bugs. Since this is a known point of confusion for analyzer authors, it makes sense to recommend one consistent way to perform the operation so all participants in the project immediately understand the intent of any given operation. The use of
|
@sharwell I feel like this might just be a case of missing information. |
@mavasani do you have an example of where we need nullable-aware comparison, and an example of where we need the default comparison to ignore it? |
Tagging @ryzngard @jasonmalinowski - they might know of the cases where the new symbol comparer is required in nullable context. |
Not necessarily. For example, the code might have not wanted |
This is unrelated to objects. this is about Symbols now having two distinct concepts of equality. strict-equality, where things like Because there are two completely valid forms of equality, we ask that people be explicit to make their code clear. This is an issue because we shipped a prior api where there was only one form of equality. However, with the introduction of NRT we now had two. Had we had NRT from the beginning, we would not have had a 'default' concept of equality here, and we would have made it a required parameter. This analyzer is to help move older code forward to use the better pattern now that there are multiple concepts of equality. |
One example. When you're implementing an interface, we need to see whihc members are already implemented. Implementations do not care if there are NRT differences. As such, we use the 'default' comparer. However, for code that is generating casts, we'll often put in a check that says "don't bother to cast if the types are hte same". i.e. no point to cast a |
@jods, i recommend taking questions over to gitter as well, you'll likely be able to get responses faster and in a more conducive conversational manner to your questions. |
@CyrusNajmabadi I see and I understand now! Right now, as a user I just read: "your code is incorrect" and I'm like: "no it's not". It would be much better if the warning said something along the lines of: That is actually actionnable. On top of disagreeing with the message, I had just no idea what I should change about it, as IEquatable looked perfectly fine to me. |
I have no problem with htat. Reactivating to track an improved error message. |
@jods4 Would you find these messages more helpful:
|
Close! If possible, it would be best if you could point what the right ways to do it are (obviously there are two options). |
@jods4 There is a code fix for this diagnostic which automates the transition to the new pattern, so there is a bit less need to be verbose about the steps to take. |
The code fix isn't always available (in my case, it crashes VS, but I'm sure that's just a bug). I just wasted quite a bit of time trying to figure out what this warning even meant. Thank goodness for this random issue, or I'd never have figured it out. The error message just should just up and say what the fix wants you to do. |
@sharwell Shall we update the message or is everything's ok now and the ticket can be closed? |
ping @sharwell |
This comment has been minimized.
This comment has been minimized.
Let's use this issue to improve the diagnostic message. |
Edit: I just saw a fix has been provided here #4571. I'm also reproing this as well when defining a dictionary with |
What am I doing wrong? As a matter of usability, could the default equality comparer implementation be made to work 'correctly' for all @carlossanlop, I believe you should be passing the correct IEqualityComparer instance to the Dictionary's constructor. |
you're probably on the wrong issue, the PR #4571 was for issue #4568 (which has been mentioned above)
@ReubenBond Same question was asked earlier above, this was issue #4568, you probably don't have the fix from PR #4571 yet |
Hello everybody, in general I understand (and like) the warining. But I do not understand why I get the warning: RS1024 when creating a HashSet in the following way: private readonly ISet<ITypeSymbol> HandledTypes = new HashSet<ITypeSymbol>(SymbolEqualityComparer.Default); How should I store multiple ITypeSymbol objects in my rewriters? I prefer an ISet instead of an IList object because of better search capabilities. Best regards, Michael |
@mbaumanns That has been confirmed above as a false-positive that has been fixed in #4568 #4845 confirms a fix has been deployed so you may want to check the version you are using? I don't know if its in a non-beta build yet, but either way its a different issue than what is discussed here. If you have to use a broken version you can selectively suppress the warning for this line of code (if you do it often you can write a helper method that does the work and suppresses the warning once) |
I have just got this warning, but I do not understand why - I do not compare any symbols:
What exactly is the problem? |
In order to work, the GroupBy operator uses Equatable.Default which look for the IEquatable interface ending up to an Equals method call for the comparison |
The code shown by @MarkKharitonov is not comparing symbols, it is comparing strings. |
@KrisVandermotten Agree, but my guess is that the Analyzer is not that smart to get the right type. |
@raffaeler - then would you not agree it is a bug in the analyzer? Should I open another github issue for it? |
I believe it is and this is the right place. |
In general if you find a new issue with an analyzer a new bug would be better than adding onto an existing issue. In this case, the latest code seems to have fixed this. The analyzer now checks for known generic constraints Here's the test code I used in [Fact, WorkItem(3427, "https://github.com/dotnet/roslyn-analyzers/issues/3427")]
public async Task RS1024_GroupBy()
{
await new VerifyCS.Test
{
TestState =
{
Sources =
{
@"
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis;
class C
{
void M(IEnumerable<IMethodSymbol> methodSymbols)
{
var dict = methodSymbols
.GroupBy(candidateSymbol => candidateSymbol.Parameters[0].Type.ToString())
.ToDictionary(g => g.Key);
}
}"
, SymbolEqualityComparerStubCSharp
},
ReferenceAssemblies = CreateNetCoreReferenceAssemblies()
}
}.RunAsync();
} |
The fixed code has not been released yet, correct? |
@MarkKharitonov I believe if you're getting them from the SDK it is in .NET 6 previews, or preview releases of the analyzers. |
After an SDK upgrade (I suppose...) I am getting new warnings. It's now triggered by dictionaries, such as new Dictionary<ITypeSymbol, string>(SymbolEqualityComparer.Default) But the warning remains and I don't know what to do. I note that the message is more clear now as it points out that It doesn't say what action we should take, for example in the dictionary case it would be helpful to say You should specify which SymbolEqualityComparer you want explicitely. Then again, I did that and the warning is still here so I dunno? |
@KrisVandermotten yes, thanks. That's exactly it. |
@ryzngard Does the .NET 6 SDK include the reworded error message as well? |
@MihailsKuzmins this looks like it should be fixed already. What version of analyzers are you using? @silkfire let me check scheduling. I haven't made changes to the text as it hasn't been high on my priority list, but if there's still time to make a change here I will. Otherwise it will go into .NET 7 SDK |
@ryzngard |
@ryzngard I believe this is not released with the SDK at all. |
@MihailsKuzmins A pre-release is available here. I think a stable version should be pushed to NuGet. cc @jmarolf |
Yea, I always forget metadata analyzers aren't in the SDK. Also means that my string fix might be in the wrong branch? Happy to |
@Youssef1313, great thank you 🙏 |
Version Used: 3.5.0
Steps to Reproduce:
Compare two
ISymbol
through theirIEquatable<ISymbol>
interfaceExpected Behavior:
No warning.
Actual Behavior:
Warning RS1024 Compare symbols correctly
The text was updated successfully, but these errors were encountered: