-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move language-specific functionality to derived types
- Loading branch information
Showing
5 changed files
with
157 additions
and
73 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
src/Roslyn.Diagnostics.Analyzers/CSharp/CSharpDoNotCopyValue.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.CodeAnalysis.Operations; | ||
using Roslyn.Diagnostics.Analyzers; | ||
|
||
namespace Roslyn.Diagnostics.CSharp.Analyzers | ||
{ | ||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public sealed class CSharpDoNotCopyValue : AbstractDoNotCopyValue | ||
{ | ||
protected override NonCopyableWalker CreateWalker(OperationBlockAnalysisContext context, NonCopyableTypesCache cache) | ||
=> new CSharpNonCopyableWalker(context, cache); | ||
|
||
private sealed class CSharpNonCopyableWalker : NonCopyableWalker | ||
{ | ||
public CSharpNonCopyableWalker(OperationBlockAnalysisContext context, NonCopyableTypesCache cache) | ||
: base(context, cache) | ||
{ | ||
} | ||
|
||
protected override bool CheckForEachGetEnumerator(IForEachLoopOperation operation, [DisallowNull] ref IConversionOperation? conversion, [DisallowNull] ref IOperation? instance) | ||
{ | ||
if (operation.Syntax is CommonForEachStatementSyntax syntax | ||
&& operation.SemanticModel.GetForEachStatementInfo(syntax).GetEnumeratorMethod is { } getEnumeratorMethod) | ||
{ | ||
CheckMethodSymbolInUnsupportedContext(operation, getEnumeratorMethod); | ||
|
||
if (instance is not null | ||
&& Cache.IsNonCopyableType(getEnumeratorMethod.ReceiverType) | ||
&& !getEnumeratorMethod.IsReadOnly | ||
&& Acquire(instance) == RefKind.In) | ||
{ | ||
// mark the instance as not checked by this method | ||
instance = null; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.