microsoft / microsoft/vs-threading
VSTHRD010 raises a false positive with eager linq queries
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 1k
- Forks
- 160
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 28
Description
Bug description
VSTHRD010 flags a false positive when the access to the object that requires the main thread is done in a linq statement that's eager.
Repro steps
See attached project :)
public static async Task<EnvDTE.Project> GetProjectAsync(EnvDTE80.Solution2 solution2, string projectName)
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
return solution2
.Projects
.Cast<EnvDTE.Project>()
.FirstOrDefault(project => project.Name.Equals(projectName, StringComparison.OrdinalIgnoreCase)); // One that can potentially be improved.
}
This incorrectly raises a warning.
By contrast this warning is correct:
public static async Task<IEnumerable<EnvDTE.Project>> GetProjectsAsync(EnvDTE80.Solution2 solution2, string projectNamePattern)
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
return solution2
.Projects
.Cast<EnvDTE.Project>()
.Where(project => project.Name.Contains(projectNamePattern)); // Correctly flagged pattern.
}
Expected behavior
No warning :)
Actual behavior
The warning was raised :)
- Version used: Microsoft.VisualStudio.Threading.(Analyzers) versions: 16.4.43
- Application (if applicable):
Additional context
This was raised in our test code: https://github.com/NuGet/NuGet.Client/blob/cb402b5a3340ab5b2605ecc72de7f70aaac95344/test/TestExtensions/API.Test/VSSolutionHelper.cs#L463-L471
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Open AnalyzerFalsePositive.zip and reproduce the warning on the shown GetProjectAsync method. Compare the eager FirstOrDefault query with the lazy Where query; done means the eager query produces no warning while the lazy query remains correctly flagged.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100