Roslyn: Avoid delegate captures for ConcurrentDictionary.AddOrUpdate

Created on 6 Feb 2019  路  4Comments  路  Source: dotnet/roslyn

Version Used:
3.0.0-beta2-19055-09

ConcurrentDictionary.AddOrUpdate is misused throughout the codebase. The anti-pattern seems to be:

ConcurrentDictionary<T1, T2> dictionary;
dictionary.AddOrUpdate(key, value, (_1, _2) => value);

The right usage would be:

ConcurrentDictionary<T1, T2> dictionary;
dictionary[key] = value;

More info on this is in this commit: https://github.com/dotnet/roslyn/pull/33173/commits/2ac11675a1785bdb58c54369f8a6cbb3c529d50e

Area-Infrastructure Concept-Continuous Improvement Resolution-Fixed

Most helpful comment

@CyrusNajmabadi Sure, but it should probably be implemented as an analyzer in dotnet/corefx or dotnet/coreclr since it's a framework usage analysis.

All 4 comments

@sharwell could we write an analyzer to catch this?

@mavasani too.

@CyrusNajmabadi Sure, but it should probably be implemented as an analyzer in dotnet/corefx or dotnet/coreclr since it's a framework usage analysis.

Opened issue to track ConcurrentDictionary analyzer: https://github.com/dotnet/roslyn-analyzers/issues/2274

Was this page helpful?
0 / 5 - 0 ratings