Skip to content

Commit

Permalink
Merge pull request #2312 from JetBrains/net221-mte-method-signature-f…
Browse files Browse the repository at this point in the history
…ixes

Fix method signature warnings and other minor warnings
  • Loading branch information
citizenmatt authored Apr 26, 2022
2 parents 81f922d + 16fa6b2 commit bb0081c
Show file tree
Hide file tree
Showing 17 changed files with 336 additions and 163 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,20 @@ Since 2018.1, the version numbers and release cycle match Rider's versions and r

## 2022.1.1
* [Commits](https://github.com/JetBrains/resharper-unity/compare/net221-rtm-2022.1.0...net221-rtm-2022.1.1)
* [Milestone](https://github.com/JetBrains/resharper-unity/milestone/52?closed=1)

### Added

- Add "means implicit use" external annotations for `OverlayAttribute` and `EditorToolbarElementAttribute` ([RIDER-76826](https://youtrack.jetbrains.com/issue/RIDER-76826), [#2312](https://github.com/JetBrains/resharper-unity/pull/2312))

### Changed

- Treat types that implement `ISystem` as ECS systems ([#2301](https://github.com/JetBrains/resharper-unity/issues/2301), [#2312](https://github.com/JetBrains/resharper-unity/pull/2312))

### Fixed

- Fix incorrect method signature warning for semi-documented `OnPostprocessAllAssets` event function overload ([RIDER-76682](https://youtrack.jetbrains.com/issue/RIDER-76682), [#2312](https://github.com/JetBrains/resharper-unity/pull/2312))
- Fix method signature warnings for `[MenuItem]` with the optional `MenuCommand` parameter, and when the `validate` argument is set via named property instead of positional argument ([RIDER-76680](https://youtrack.jetbrains.com/issue/RIDER-76680), [RIDER-76681](https://youtrack.jetbrains.com/issue/RIDER-76681), [#2312](https://github.com/JetBrains/resharper-unity/pull/2312))
- Fix exception when asset reference a missing script ([DEXP-661796](https://youtrack.jetbrains.com/issue/DEXP-661796))


Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using System.Collections.Concurrent;
#nullable enable

using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using JetBrains.Metadata.Reader.API;
using JetBrains.ReSharper.Feature.Services.Daemon;
using JetBrains.ReSharper.Plugins.Unity.CSharp.Daemon.Errors;
using JetBrains.ReSharper.Plugins.Unity.UnityEditorIntegration.Api;
using JetBrains.ReSharper.Psi;
using JetBrains.ReSharper.Psi.CSharp.Parsing;
using JetBrains.ReSharper.Psi.CSharp.Tree;
using JetBrains.ReSharper.Psi.Tree;
using JetBrains.Util.dataStructures;
using MethodSignature = JetBrains.ReSharper.Plugins.Unity.UnityEditorIntegration.Api.MethodSignature;

Expand Down Expand Up @@ -90,10 +91,9 @@ protected override void Analyze(IAttribute element, ElementProblemAnalyzerData d
}
}

[CanBeNull]
private MethodSignature[] GetExpectedMethodSignatures(ITypeElement attributeTypeElement,
IAttribute attribute,
PredefinedType predefinedType)
private MethodSignature[]? GetExpectedMethodSignatures(ITypeElement attributeTypeElement,
IAttribute attribute,
PredefinedType predefinedType)
{
var attributeClrName = attributeTypeElement.GetClrName();
if (myMethodSignatures.TryGetValue(attributeClrName, out var signatures))
Expand All @@ -117,8 +117,7 @@ private MethodSignature[] GetExpectedMethodSignatures(ITypeElement attributeType
return signatures;
}

[CanBeNull]
private MethodSignature[] GetSignaturesFromRequiredSignatureAttribute(ITypeElement attributeTypeElement)
private MethodSignature[]? GetSignaturesFromRequiredSignatureAttribute(ITypeElement attributeTypeElement)
{
var signatures = new FrugalLocalList<MethodSignature>();
foreach (var method in attributeTypeElement.Methods)
Expand All @@ -141,9 +140,8 @@ private MethodSignature[] GetSignaturesFromRequiredSignatureAttribute(ITypeEleme
return signatures.IsEmpty ? null : signatures.ToArray();
}

[CanBeNull]
private MethodSignature[] GetSignaturesFromKnownAttributes(IClrTypeName attributeClrName,
PredefinedType predefinedType)
private MethodSignature[]? GetSignaturesFromKnownAttributes(IClrTypeName attributeClrName,
PredefinedType predefinedType)
{
if (ourKnownAttributes.Contains(attributeClrName))
{
Expand All @@ -156,48 +154,62 @@ private MethodSignature[] GetSignaturesFromKnownAttributes(IClrTypeName attribut
return null;
}

[CanBeNull]
private MethodSignature[] GetSpecialCaseSignatures(
IClrTypeName attributeClrName, PredefinedType predefinedType)
private MethodSignature[]? GetSpecialCaseSignatures(IClrTypeName attributeClrName,
PredefinedType predefinedType)
{
if (Equals(attributeClrName, KnownTypes.OnOpenAssetAttribute))
return GetOnOpenAssetMethodSignature(predefinedType);
if (Equals(attributeClrName, KnownTypes.PostProcessBuildAttribute))
return GetPostProcessBuildMethodSignature(predefinedType);
return null;
}
[CanBeNull]
private MethodSignature[] GetNonCacheableSignatures(IAttribute attribute,
IClrTypeName attributeClrName, PredefinedType predefinedType)

private MethodSignature[]? GetNonCacheableSignatures(IAttribute attribute,
IClrTypeName attributeClrName,
PredefinedType predefinedType)
{
if (Equals(attributeClrName, KnownTypes.MenuItemAttribute))
return GetMenuItemMethodSignature(attribute, predefinedType);
return null;
}

private static MethodSignature GetStaticVoidMethodSignature(PredefinedType predefinedType) =>
new(predefinedType.Void, true);

private MethodSignature[] GetMenuItemMethodSignature(IAttribute attribute, PredefinedType predefinedType)
{
if (attribute.Arguments.Count <= 1) // MenuItem("text")
return new[] { new MethodSignature(predefinedType.Void, true) };
var secondParameter = attribute.Arguments[1].FirstChild?.FirstChild;
if (secondParameter != null)
IExpression? validateArgExpression = null;
if (attribute.Arguments.Count > 1)
{
// MenuItem("text", true)
if (secondParameter.NodeType.Equals(CSharpTokenType.TRUE_KEYWORD))
return new[] { new MethodSignature(predefinedType.Bool, true) };
// MenuItem("text", false)
if (secondParameter.NodeType.Equals(CSharpTokenType.FALSE_KEYWORD))
return new[] { new MethodSignature(predefinedType.Void, true) };
// [MenuItem("Something", true|false)]
validateArgExpression = attribute.Arguments[1].Expression;
}
else
{
// [MenuItem("Something", validate = true|false)]
foreach (var assignment in attribute.PropertyAssignments)
{
if (assignment.PropertyNameIdentifier?.Name == "validate")
{
validateArgExpression = assignment.Source;
break;
}
}
}

return null;
}

[NotNull]
private static MethodSignature GetStaticVoidMethodSignature(PredefinedType predefinedType)
{
return new MethodSignature(predefinedType.Void, true);
var returnType = predefinedType.Void;
if (validateArgExpression != null && validateArgExpression.IsConstantValue() &&
validateArgExpression.ConstantValue.IsBoolean(out var validate) && validate)
{
returnType = predefinedType.Bool;
}

var menuCommandType = myKnownTypesCache.GetByClrTypeName(KnownTypes.MenuCommand, predefinedType.Module);
return new[]
{
new MethodSignature(returnType, true),
new MethodSignature(returnType, true, new[] { menuCommandType }, new[] { "menuCommand" })
};
}

private static MethodSignature[] GetOnOpenAssetMethodSignature(PredefinedType predefinedType)
Expand Down Expand Up @@ -225,4 +237,4 @@ private MethodSignature[] GetPostProcessBuildMethodSignature(PredefinedType pred
};
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,19 @@ public override bool AddDeclarationHighlighting(IDeclaration node, IHighlighting
}
else if (typeElement.DerivesFrom(KnownTypes.Editor) || typeElement.DerivesFrom(KnownTypes.EditorWindow))
{
AddEditorHighlighting(consumer, element, "Editor", "Custom Unity Editor", context);
AddEditorHighlighting(consumer, element, "Editor", "Custom Unity editor", context);
}
else if (typeElement.DerivesFromScriptableObject())
{
AddMonoBehaviourHighlighting(consumer, element, "Scriptable object", "Scriptable Object", context);
AddMonoBehaviourHighlighting(consumer, element, "Scriptable object", "Unity scriptable object", context);
}
else if (myUnityApi.IsUnityType(typeElement))
{
AddUnityTypeHighlighting(consumer, element, "Unity type", "Custom Unity type", context);
}
else if (myUnityApi.IsComponentSystemType(typeElement))
else if (UnityApi.IsDotsSystemType(typeElement))
{
AddUnityECSHighlighting(consumer, element, "Unity ECS", "Unity entity component system object",
AddUnityECSHighlighting(consumer, element, "ECS system", "Unity entities system",
context);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
#nullable enable

using System.Collections.Generic;
using JetBrains.Annotations;
using JetBrains.Application;
using JetBrains.Metadata.Reader.API;
Expand Down Expand Up @@ -52,7 +54,7 @@ public bool SuppressUsageInspectionsOnElement(IDeclaredElement element, out Impl
switch (element)
{
case IClass cls when unityApi.IsUnityType(cls) ||
unityApi.IsComponentSystemType(cls) ||
UnityApi.IsDotsSystemType(cls) ||
IsUxmlFactory(cls):
flags = ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature;
return true;
Expand Down Expand Up @@ -110,9 +112,7 @@ public bool SuppressUsageInspectionsOnElement(IDeclaredElement element, out Impl
private bool IsUxmlFactory(IClass cls)
{
var baseClass = cls.GetBaseClassType();
if (baseClass == null)
return false;
return myUxmlFactoryBaseClasses.Contains(baseClass.GetClrName());
return baseClass != null && myUxmlFactoryBaseClasses.Contains(baseClass.GetClrName());
}

private static bool IsAnimationEvent(ISolution solution, IDeclaredElement property)
Expand Down Expand Up @@ -155,7 +155,7 @@ private bool IsImplicitlyUsedInterfaceProperty(IProperty property)
return false;
}

private bool HasOptionalParameter(UnityEventFunction function)
private static bool HasOptionalParameter(UnityEventFunction function)
{
foreach (var parameter in function.Parameters)
{
Expand All @@ -166,12 +166,12 @@ private bool HasOptionalParameter(UnityEventFunction function)
return false;
}

private bool IsEventHandler(UnityApi unityApi, [CanBeNull] IMethod method)
private static bool IsEventHandler(UnityApi unityApi, IMethod? method)
{
if (method == null)
return false;

var type = method.GetContainingType();
var type = method.ContainingType;
if (!unityApi.IsUnityType(type))
return false;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using JetBrains.Metadata.Reader.API;
#nullable enable

using System.Diagnostics.CodeAnalysis;
using JetBrains.Metadata.Reader.API;
using JetBrains.Metadata.Reader.Impl;

namespace JetBrains.ReSharper.Plugins.Unity.UnityEditorIntegration.Api
{
[SuppressMessage("ReSharper", "InconsistentNaming")]
public static class KnownTypes
{
// System
Expand Down Expand Up @@ -81,10 +85,11 @@ public static class KnownTypes
public static readonly IClrTypeName GizmoType = new ClrTypeName("UnityEditor.GizmoType");
public static readonly IClrTypeName InitializeOnLoadAttribute = new ClrTypeName("UnityEditor.InitializeOnLoadAttribute");
public static readonly IClrTypeName InitializeOnLoadMethodAttribute = new ClrTypeName("UnityEditor.InitializeOnLoadMethodAttribute");
public static readonly IClrTypeName MenuCommand = new ClrTypeName("UnityEditor.MenuCommand");
public static readonly IClrTypeName MenuItemAttribute = new ClrTypeName("UnityEditor.MenuItem");
public static readonly IClrTypeName PreferenceItem = new ClrTypeName("UnityEditor.PreferenceItem");
public static readonly IClrTypeName PropertyDrawer = new ClrTypeName("UnityEditor.PropertyDrawer");
public static readonly IClrTypeName RequiredSignatureAttribute = new ClrTypeName("UnityEditor.RequiredSignatureAttribute");
public static readonly IClrTypeName MenuItemAttribute = new ClrTypeName("UnityEditor.MenuItem");

// UnityEditor.Callbacks
public static readonly IClrTypeName DidReloadScripts = new ClrTypeName("UnityEditor.Callbacks.DidReloadScripts");
Expand All @@ -98,7 +103,7 @@ public static class KnownTypes

// ECS/DOTS
public static readonly IClrTypeName ComponentSystemBase = new ClrTypeName("Unity.Entities.ComponentSystemBase");
public static readonly IClrTypeName JobComponentSystem = new ClrTypeName("Unity.Entities.JobComponentSystem");
public static readonly IClrTypeName ISystem = new ClrTypeName("Unity.Entities.ISystem");

// Burst
public static readonly IClrTypeName BurstCompiler = new ClrTypeName("Unity.Burst.BurstCompiler");
Expand All @@ -109,13 +114,13 @@ public static class KnownTypes
public static readonly IClrTypeName SharedStatic = new ClrTypeName("Unity.Burst.SharedStatic`1");

// Jobs
public static readonly IClrTypeName Job = new ClrTypeName("Unity.Jobs.IJob");
public static readonly IClrTypeName JobFor = new ClrTypeName("Unity.Jobs.IJobFor");
public static readonly IClrTypeName JobParallelFor = new ClrTypeName("Unity.Jobs.IJobParallelFor");
public static readonly IClrTypeName AnimationJob = new ClrTypeName("UnityEngine.Animations.IAnimationJob");
public static readonly IClrTypeName JobParallelForTransform = new ClrTypeName("UnityEngine.Jobs.IJobParallelForTransform");
public static readonly IClrTypeName JobParticleSystem = new ClrTypeName("UnityEngine.ParticleSystemJobs.IJobParticleSystem");
public static readonly IClrTypeName JobParticleSystemParallelFor = new ClrTypeName("UnityEngine.ParticleSystemJobs.IJobParticleSystemParallelFor");
public static readonly IClrTypeName JobParticleSystemParallelForBatch = new ClrTypeName("UnityEngine.ParticleSystemJobs.IJobParticleSystemParallelForBatch");
// public static readonly IClrTypeName Job = new ClrTypeName("Unity.Jobs.IJob");
// public static readonly IClrTypeName JobFor = new ClrTypeName("Unity.Jobs.IJobFor");
// public static readonly IClrTypeName JobParallelFor = new ClrTypeName("Unity.Jobs.IJobParallelFor");
// public static readonly IClrTypeName AnimationJob = new ClrTypeName("UnityEngine.Animations.IAnimationJob");
// public static readonly IClrTypeName JobParallelForTransform = new ClrTypeName("UnityEngine.Jobs.IJobParallelForTransform");
// public static readonly IClrTypeName JobParticleSystem = new ClrTypeName("UnityEngine.ParticleSystemJobs.IJobParticleSystem");
// public static readonly IClrTypeName JobParticleSystemParallelFor = new ClrTypeName("UnityEngine.ParticleSystemJobs.IJobParticleSystemParallelFor");
// public static readonly IClrTypeName JobParticleSystemParallelForBatch = new ClrTypeName("UnityEngine.ParticleSystemJobs.IJobParticleSystemParallelForBatch");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,11 @@ public UnityApi(UnityVersion unityVersion, UnityTypeCache unityTypeCache, UnityT
myKnownTypesCache = knownTypesCache;
}

public bool IsUnityType([NotNullWhen(true)] ITypeElement? type) => type != null && myUnityTypeCache.IsUnityType(type);
public bool IsUnityType([NotNullWhen(true)] ITypeElement? type) =>
type != null && myUnityTypeCache.IsUnityType(type);

public bool IsComponentSystemType([NotNullWhen(true)] ITypeElement? typeElement)
{
// This covers ComponentSystem, JobComponentSystem and SystemBase
return typeElement.DerivesFrom(KnownTypes.ComponentSystemBase);
}
public static bool IsDotsSystemType([NotNullWhen(true)] ITypeElement? typeElement) =>
typeElement.DerivesFrom(KnownTypes.ComponentSystemBase) || typeElement.DerivesFrom(KnownTypes.ISystem);

// A serialised field cannot be abstract or generic, but a type declaration that will be serialised can be. This
// method differentiates between a type declaration and a type usage. Consider renaming if we ever need to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
</parameters>
<returns type="UnityEngine.Material" array="False" />
</message>
<message name="OnPostprocessAllAssets" static="True" maximumVersion="2021.1" description="This is called after importing of any number of assets is complete (when the Assets progress bar has reached the end)." path="Documentation/en/ScriptReference/AssetPostprocessor.OnPostprocessAllAssets.html">
<message name="OnPostprocessAllAssets" static="True" description="This is called after importing of any number of assets is complete (when the Assets progress bar has reached the end)." path="Documentation/en/ScriptReference/AssetPostprocessor.OnPostprocessAllAssets.html">
<parameters>
<parameter type="System.String" array="True" name="importedAssets" />
<parameter type="System.String" array="True" name="deletedAssets" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@
</attribute>
</member>

<!-- N:UnityEditor.Overlays -->
<member name="T:UnityEditor.Overlays.OverlayAttribute">
<attribute ctor="M:JetBrains.Annotations.MeansImplicitUseAttribute.#ctor" />
<attribute ctor="M:JetBrains.Annotations.BaseTypeRequiredAttribute.#ctor(System.Type)">
<argument>UnityEditor.Overlays.Overlay</argument>
</attribute>
</member>

<!-- N:UnityEditor.Rendering -->
<member name="T:UnityEditor.Rendering.ScriptableRenderPipelineExtensionAttribute">
<attribute ctor="M:JetBrains.Annotations.MeansImplicitUseAttribute.#ctor" />
Expand All @@ -107,4 +115,12 @@
<member name="T:UnityEditor.ShortcutManagement.ClutchShortcutAttribute">
<attribute ctor="M:JetBrains.Annotations.MeansImplicitUseAttribute.#ctor" />
</member>

<!-- N:UnityEditor.Toolbars -->
<member name="T:UnityEditor.Toolbars.EditorToolbarElementAttribute">
<attribute ctor="M:JetBrains.Annotations.MeansImplicitUseAttribute.#ctor" />
<attribute ctor="M:JetBrains.Annotations.BaseTypeRequiredAttribute.#ctor(System.Type)">
<argument>UnityEngine.UIElements.VisualElement</argument>
</attribute>
</member>
</assembly>
Loading

0 comments on commit bb0081c

Please sign in to comment.