Added rule regarding the random class

This commit is contained in:
Thorsten Sommer 2025-02-24 19:35:40 +01:00
parent 5e5ff00f77
commit 60b58648ca
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
3 changed files with 58 additions and 1 deletions

View File

@ -6,4 +6,5 @@
-----------|----------|----------|------------------------ -----------|----------|----------|------------------------
MWAIS0001 | Usage | Error | ProviderAccessAnalyzer MWAIS0001 | Usage | Error | ProviderAccessAnalyzer
MWAIS0002 | Naming | Error | ConstStaticAnalyzer MWAIS0002 | Naming | Error | ConstStaticAnalyzer
MWAIS0003 | Naming | Error | UnderscorePrefixAnalyzer MWAIS0003 | Naming | Error | UnderscorePrefixAnalyzer
MWAIS0004 | Usage | Error | RandomInstantiationAnalyzer

View File

@ -5,4 +5,5 @@ public static class Identifier
public const string PROVIDER_ACCESS_ANALYZER = $"{Tools.ID_PREFIX}0001"; public const string PROVIDER_ACCESS_ANALYZER = $"{Tools.ID_PREFIX}0001";
public const string CONST_STATIC_ANALYZER = $"{Tools.ID_PREFIX}0002"; public const string CONST_STATIC_ANALYZER = $"{Tools.ID_PREFIX}0002";
public const string UNDERSCORE_PREFIX_ANALYZER = $"{Tools.ID_PREFIX}0003"; public const string UNDERSCORE_PREFIX_ANALYZER = $"{Tools.ID_PREFIX}0003";
public const string RANDOM_INSTANTIATION_ANALYZER = $"{Tools.ID_PREFIX}0004";
} }

View File

@ -0,0 +1,55 @@
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
namespace SourceCodeRules.UsageAnalyzers;
#pragma warning disable RS1038
[DiagnosticAnalyzer(LanguageNames.CSharp)]
#pragma warning restore RS1038
public class RandomInstantiationAnalyzer : DiagnosticAnalyzer
{
private const string DIAGNOSTIC_ID = Identifier.RANDOM_INSTANTIATION_ANALYZER;
private static readonly string TITLE = "Direct instantiation of Random is not allowed";
private static readonly string MESSAGE_FORMAT = "Do not use 'new Random()'. Instead, inject and use the ThreadSafeRandom service from the DI container.";
private static readonly string DESCRIPTION = "Using 'new Random()' can lead to issues in multi-threaded scenarios. Use the ThreadSafeRandom service instead.";
private const string CATEGORY = "Usage";
private static readonly DiagnosticDescriptor RULE = new(
DIAGNOSTIC_ID,
TITLE,
MESSAGE_FORMAT,
CATEGORY,
DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: DESCRIPTION);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => [RULE];
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.EnableConcurrentExecution();
context.RegisterSyntaxNodeAction(this.AnalyzeObjectCreation, SyntaxKind.ObjectCreationExpression);
}
private void AnalyzeObjectCreation(SyntaxNodeAnalysisContext context)
{
var objectCreation = (ObjectCreationExpressionSyntax)context.Node;
if (context.SemanticModel.GetSymbolInfo(objectCreation.Type).Symbol is not ITypeSymbol typeSymbol)
return;
if (typeSymbol.ToString() == "System.Random" || typeSymbol is { Name: "Random", ContainingNamespace.Name: "System" })
{
var diagnostic = Diagnostic.Create(RULE, objectCreation.GetLocation());
context.ReportDiagnostic(diagnostic);
}
}
}