in resharper/resharper-unity/src/Unity/CSharp/Daemon/Stages/Analysis/InstantiateWithoutParentProblemAnalyzer.cs [30:103]
protected override void Analyze(IInvocationExpression expression, ElementProblemAnalyzerData data, IHighlightingConsumer consumer)
{
var reference = (expression.InvokedExpression as IReferenceExpression)?.Reference;
if (reference == null)
return;
var info = reference.Resolve();
if (info.ResolveErrorType != ResolveErrorType.OK)
return;
var method = info.DeclaredElement as IMethod;
if (method is not { ShortName: KnownMethod })
return;
if (method.ContainingType?.GetClrName().Equals(KnownTypes.Object) != true)
return;
var parameters = method.Parameters;
if (parameters.Count != 1)
return;
var scope = expression.GetContainingFunctionLikeDeclarationOrClosure().GetCodeBody().GetAnyTreeNode();
if (scope == null)
return;
IEnumerable<ITreeNode>? usages;
ITreeNode? storage;
var containingParenthesizedExpression = expression.GetContainingParenthesizedExpression();
var castExpression = CastExpressionNavigator.GetByOp(containingParenthesizedExpression);
var asExpression = AsExpressionNavigator.GetByOperand(containingParenthesizedExpression);
var initializer = ExpressionInitializerNavigator.GetByValue(castExpression ?? asExpression ?? containingParenthesizedExpression);
var declaration = LocalVariableDeclarationNavigator.GetByInitial(initializer);
var usageProvider = data.GetUsagesProvider();
if (declaration != null)
{
usages = usageProvider.GetUsages(declaration.DeclaredElement, scope).Where(t => t is IReferenceExpression);
storage = declaration;
}
else
{
var assignment = AssignmentExpressionNavigator.GetBySource(castExpression ?? asExpression ?? containingParenthesizedExpression);
var dest = assignment?.Dest as IReferenceExpression;
var destInfo = dest?.Reference.Resolve();
if (dest != null && destInfo != null && destInfo.ResolveErrorType == ResolveErrorType.OK)
{
usages = usageProvider.GetUsages(destInfo.DeclaredElement.NotNull(), scope).Where(t => IsSameReferenceUsed(t, dest));
storage = dest;
}
else
{
return;
}
}
foreach (var usage in usages)
{
if (usage is IReferenceExpression referenceExpression)
{
var fullReferenceExpression = ReferenceExpressionNavigator.GetTopByQualifierExpression(referenceExpression);
if (IsUsageSetTransformParent(fullReferenceExpression, out var stayInWorldCoords, out var transform))
{
if (!InSameBlock(fullReferenceExpression, storage))
return;
var finder = new TransformParentRelatedReferenceFinder(referenceExpression);
var relatedExpressions = finder.GetRelatedExpressions(scope, expression).FirstOrDefault();
if (relatedExpressions == null || relatedExpressions.GetTreeStartOffset() >= fullReferenceExpression.GetTreeStartOffset())
consumer.AddHighlighting(new InstantiateWithoutParentWarning(fullReferenceExpression, expression, transform, stayInWorldCoords));
return;
}
}
}
}