Skip to content

Commit bb0081c

Browse files
authored
Merge pull request #2312 from JetBrains/net221-mte-method-signature-fixes
Fix method signature warnings and other minor warnings
2 parents 81f922d + 16fa6b2 commit bb0081c

File tree

17 files changed

+336
-163
lines changed

17 files changed

+336
-163
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,20 @@ Since 2018.1, the version numbers and release cycle match Rider's versions and r
1010

1111
## 2022.1.1
1212
* [Commits](https://github.com/JetBrains/resharper-unity/compare/net221-rtm-2022.1.0...net221-rtm-2022.1.1)
13+
* [Milestone](https://github.com/JetBrains/resharper-unity/milestone/52?closed=1)
14+
15+
### Added
16+
17+
- 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))
18+
19+
### Changed
20+
21+
- 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))
1322

1423
### Fixed
1524

25+
- 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))
26+
- 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))
1627
- Fix exception when asset reference a missing script ([DEXP-661796](https://youtrack.jetbrains.com/issue/DEXP-661796))
1728

1829

resharper/resharper-unity/src/Unity/CSharp/Daemon/Stages/Analysis/AttributedMethodSignatureProblemAnalyzer.cs

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
using System.Collections.Concurrent;
1+
#nullable enable
2+
3+
using System.Collections.Concurrent;
24
using System.Collections.Generic;
35
using System.Linq;
4-
using JetBrains.Annotations;
56
using JetBrains.Metadata.Reader.API;
67
using JetBrains.ReSharper.Feature.Services.Daemon;
78
using JetBrains.ReSharper.Plugins.Unity.CSharp.Daemon.Errors;
89
using JetBrains.ReSharper.Plugins.Unity.UnityEditorIntegration.Api;
910
using JetBrains.ReSharper.Psi;
10-
using JetBrains.ReSharper.Psi.CSharp.Parsing;
1111
using JetBrains.ReSharper.Psi.CSharp.Tree;
12+
using JetBrains.ReSharper.Psi.Tree;
1213
using JetBrains.Util.dataStructures;
1314
using MethodSignature = JetBrains.ReSharper.Plugins.Unity.UnityEditorIntegration.Api.MethodSignature;
1415

@@ -90,10 +91,9 @@ protected override void Analyze(IAttribute element, ElementProblemAnalyzerData d
9091
}
9192
}
9293

93-
[CanBeNull]
94-
private MethodSignature[] GetExpectedMethodSignatures(ITypeElement attributeTypeElement,
95-
IAttribute attribute,
96-
PredefinedType predefinedType)
94+
private MethodSignature[]? GetExpectedMethodSignatures(ITypeElement attributeTypeElement,
95+
IAttribute attribute,
96+
PredefinedType predefinedType)
9797
{
9898
var attributeClrName = attributeTypeElement.GetClrName();
9999
if (myMethodSignatures.TryGetValue(attributeClrName, out var signatures))
@@ -117,8 +117,7 @@ private MethodSignature[] GetExpectedMethodSignatures(ITypeElement attributeType
117117
return signatures;
118118
}
119119

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

144-
[CanBeNull]
145-
private MethodSignature[] GetSignaturesFromKnownAttributes(IClrTypeName attributeClrName,
146-
PredefinedType predefinedType)
143+
private MethodSignature[]? GetSignaturesFromKnownAttributes(IClrTypeName attributeClrName,
144+
PredefinedType predefinedType)
147145
{
148146
if (ourKnownAttributes.Contains(attributeClrName))
149147
{
@@ -156,48 +154,62 @@ private MethodSignature[] GetSignaturesFromKnownAttributes(IClrTypeName attribut
156154
return null;
157155
}
158156

159-
[CanBeNull]
160-
private MethodSignature[] GetSpecialCaseSignatures(
161-
IClrTypeName attributeClrName, PredefinedType predefinedType)
157+
private MethodSignature[]? GetSpecialCaseSignatures(IClrTypeName attributeClrName,
158+
PredefinedType predefinedType)
162159
{
163160
if (Equals(attributeClrName, KnownTypes.OnOpenAssetAttribute))
164161
return GetOnOpenAssetMethodSignature(predefinedType);
165162
if (Equals(attributeClrName, KnownTypes.PostProcessBuildAttribute))
166163
return GetPostProcessBuildMethodSignature(predefinedType);
167164
return null;
168165
}
169-
170-
[CanBeNull]
171-
private MethodSignature[] GetNonCacheableSignatures(IAttribute attribute,
172-
IClrTypeName attributeClrName, PredefinedType predefinedType)
166+
167+
private MethodSignature[]? GetNonCacheableSignatures(IAttribute attribute,
168+
IClrTypeName attributeClrName,
169+
PredefinedType predefinedType)
173170
{
174171
if (Equals(attributeClrName, KnownTypes.MenuItemAttribute))
175172
return GetMenuItemMethodSignature(attribute, predefinedType);
176173
return null;
177174
}
178175

176+
private static MethodSignature GetStaticVoidMethodSignature(PredefinedType predefinedType) =>
177+
new(predefinedType.Void, true);
178+
179179
private MethodSignature[] GetMenuItemMethodSignature(IAttribute attribute, PredefinedType predefinedType)
180180
{
181-
if (attribute.Arguments.Count <= 1) // MenuItem("text")
182-
return new[] { new MethodSignature(predefinedType.Void, true) };
183-
var secondParameter = attribute.Arguments[1].FirstChild?.FirstChild;
184-
if (secondParameter != null)
181+
IExpression? validateArgExpression = null;
182+
if (attribute.Arguments.Count > 1)
185183
{
186-
// MenuItem("text", true)
187-
if (secondParameter.NodeType.Equals(CSharpTokenType.TRUE_KEYWORD))
188-
return new[] { new MethodSignature(predefinedType.Bool, true) };
189-
// MenuItem("text", false)
190-
if (secondParameter.NodeType.Equals(CSharpTokenType.FALSE_KEYWORD))
191-
return new[] { new MethodSignature(predefinedType.Void, true) };
184+
// [MenuItem("Something", true|false)]
185+
validateArgExpression = attribute.Arguments[1].Expression;
186+
}
187+
else
188+
{
189+
// [MenuItem("Something", validate = true|false)]
190+
foreach (var assignment in attribute.PropertyAssignments)
191+
{
192+
if (assignment.PropertyNameIdentifier?.Name == "validate")
193+
{
194+
validateArgExpression = assignment.Source;
195+
break;
196+
}
197+
}
192198
}
193-
194-
return null;
195-
}
196199

197-
[NotNull]
198-
private static MethodSignature GetStaticVoidMethodSignature(PredefinedType predefinedType)
199-
{
200-
return new MethodSignature(predefinedType.Void, true);
200+
var returnType = predefinedType.Void;
201+
if (validateArgExpression != null && validateArgExpression.IsConstantValue() &&
202+
validateArgExpression.ConstantValue.IsBoolean(out var validate) && validate)
203+
{
204+
returnType = predefinedType.Bool;
205+
}
206+
207+
var menuCommandType = myKnownTypesCache.GetByClrTypeName(KnownTypes.MenuCommand, predefinedType.Module);
208+
return new[]
209+
{
210+
new MethodSignature(returnType, true),
211+
new MethodSignature(returnType, true, new[] { menuCommandType }, new[] { "menuCommand" })
212+
};
201213
}
202214

203215
private static MethodSignature[] GetOnOpenAssetMethodSignature(PredefinedType predefinedType)
@@ -225,4 +237,4 @@ private MethodSignature[] GetPostProcessBuildMethodSignature(PredefinedType pred
225237
};
226238
}
227239
}
228-
}
240+
}

resharper/resharper-unity/src/Unity/CSharp/Daemon/Stages/Highlightings/IconsProviders/TypeDetector.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,19 @@ public override bool AddDeclarationHighlighting(IDeclaration node, IHighlighting
4646
}
4747
else if (typeElement.DerivesFrom(KnownTypes.Editor) || typeElement.DerivesFrom(KnownTypes.EditorWindow))
4848
{
49-
AddEditorHighlighting(consumer, element, "Editor", "Custom Unity Editor", context);
49+
AddEditorHighlighting(consumer, element, "Editor", "Custom Unity editor", context);
5050
}
5151
else if (typeElement.DerivesFromScriptableObject())
5252
{
53-
AddMonoBehaviourHighlighting(consumer, element, "Scriptable object", "Scriptable Object", context);
53+
AddMonoBehaviourHighlighting(consumer, element, "Scriptable object", "Unity scriptable object", context);
5454
}
5555
else if (myUnityApi.IsUnityType(typeElement))
5656
{
5757
AddUnityTypeHighlighting(consumer, element, "Unity type", "Custom Unity type", context);
5858
}
59-
else if (myUnityApi.IsComponentSystemType(typeElement))
59+
else if (UnityApi.IsDotsSystemType(typeElement))
6060
{
61-
AddUnityECSHighlighting(consumer, element, "Unity ECS", "Unity entity component system object",
61+
AddUnityECSHighlighting(consumer, element, "ECS system", "Unity entities system",
6262
context);
6363
}
6464

resharper/resharper-unity/src/Unity/CSharp/Daemon/UsageChecking/UsageInspectionsSuppressor.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System.Collections.Generic;
1+
#nullable enable
2+
3+
using System.Collections.Generic;
24
using JetBrains.Annotations;
35
using JetBrains.Application;
46
using JetBrains.Metadata.Reader.API;
@@ -52,7 +54,7 @@ public bool SuppressUsageInspectionsOnElement(IDeclaredElement element, out Impl
5254
switch (element)
5355
{
5456
case IClass cls when unityApi.IsUnityType(cls) ||
55-
unityApi.IsComponentSystemType(cls) ||
57+
UnityApi.IsDotsSystemType(cls) ||
5658
IsUxmlFactory(cls):
5759
flags = ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature;
5860
return true;
@@ -110,9 +112,7 @@ public bool SuppressUsageInspectionsOnElement(IDeclaredElement element, out Impl
110112
private bool IsUxmlFactory(IClass cls)
111113
{
112114
var baseClass = cls.GetBaseClassType();
113-
if (baseClass == null)
114-
return false;
115-
return myUxmlFactoryBaseClasses.Contains(baseClass.GetClrName());
115+
return baseClass != null && myUxmlFactoryBaseClasses.Contains(baseClass.GetClrName());
116116
}
117117

118118
private static bool IsAnimationEvent(ISolution solution, IDeclaredElement property)
@@ -155,7 +155,7 @@ private bool IsImplicitlyUsedInterfaceProperty(IProperty property)
155155
return false;
156156
}
157157

158-
private bool HasOptionalParameter(UnityEventFunction function)
158+
private static bool HasOptionalParameter(UnityEventFunction function)
159159
{
160160
foreach (var parameter in function.Parameters)
161161
{
@@ -166,12 +166,12 @@ private bool HasOptionalParameter(UnityEventFunction function)
166166
return false;
167167
}
168168

169-
private bool IsEventHandler(UnityApi unityApi, [CanBeNull] IMethod method)
169+
private static bool IsEventHandler(UnityApi unityApi, IMethod? method)
170170
{
171171
if (method == null)
172172
return false;
173173

174-
var type = method.GetContainingType();
174+
var type = method.ContainingType;
175175
if (!unityApi.IsUnityType(type))
176176
return false;
177177

resharper/resharper-unity/src/Unity/UnityEditorIntegration/Api/KnownTypes.cs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
using JetBrains.Metadata.Reader.API;
1+
#nullable enable
2+
3+
using System.Diagnostics.CodeAnalysis;
4+
using JetBrains.Metadata.Reader.API;
25
using JetBrains.Metadata.Reader.Impl;
36

47
namespace JetBrains.ReSharper.Plugins.Unity.UnityEditorIntegration.Api
58
{
9+
[SuppressMessage("ReSharper", "InconsistentNaming")]
610
public static class KnownTypes
711
{
812
// System
@@ -81,10 +85,11 @@ public static class KnownTypes
8185
public static readonly IClrTypeName GizmoType = new ClrTypeName("UnityEditor.GizmoType");
8286
public static readonly IClrTypeName InitializeOnLoadAttribute = new ClrTypeName("UnityEditor.InitializeOnLoadAttribute");
8387
public static readonly IClrTypeName InitializeOnLoadMethodAttribute = new ClrTypeName("UnityEditor.InitializeOnLoadMethodAttribute");
88+
public static readonly IClrTypeName MenuCommand = new ClrTypeName("UnityEditor.MenuCommand");
89+
public static readonly IClrTypeName MenuItemAttribute = new ClrTypeName("UnityEditor.MenuItem");
8490
public static readonly IClrTypeName PreferenceItem = new ClrTypeName("UnityEditor.PreferenceItem");
8591
public static readonly IClrTypeName PropertyDrawer = new ClrTypeName("UnityEditor.PropertyDrawer");
8692
public static readonly IClrTypeName RequiredSignatureAttribute = new ClrTypeName("UnityEditor.RequiredSignatureAttribute");
87-
public static readonly IClrTypeName MenuItemAttribute = new ClrTypeName("UnityEditor.MenuItem");
8893

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

99104
// ECS/DOTS
100105
public static readonly IClrTypeName ComponentSystemBase = new ClrTypeName("Unity.Entities.ComponentSystemBase");
101-
public static readonly IClrTypeName JobComponentSystem = new ClrTypeName("Unity.Entities.JobComponentSystem");
106+
public static readonly IClrTypeName ISystem = new ClrTypeName("Unity.Entities.ISystem");
102107

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

111116
// Jobs
112-
public static readonly IClrTypeName Job = new ClrTypeName("Unity.Jobs.IJob");
113-
public static readonly IClrTypeName JobFor = new ClrTypeName("Unity.Jobs.IJobFor");
114-
public static readonly IClrTypeName JobParallelFor = new ClrTypeName("Unity.Jobs.IJobParallelFor");
115-
public static readonly IClrTypeName AnimationJob = new ClrTypeName("UnityEngine.Animations.IAnimationJob");
116-
public static readonly IClrTypeName JobParallelForTransform = new ClrTypeName("UnityEngine.Jobs.IJobParallelForTransform");
117-
public static readonly IClrTypeName JobParticleSystem = new ClrTypeName("UnityEngine.ParticleSystemJobs.IJobParticleSystem");
118-
public static readonly IClrTypeName JobParticleSystemParallelFor = new ClrTypeName("UnityEngine.ParticleSystemJobs.IJobParticleSystemParallelFor");
119-
public static readonly IClrTypeName JobParticleSystemParallelForBatch = new ClrTypeName("UnityEngine.ParticleSystemJobs.IJobParticleSystemParallelForBatch");
117+
// public static readonly IClrTypeName Job = new ClrTypeName("Unity.Jobs.IJob");
118+
// public static readonly IClrTypeName JobFor = new ClrTypeName("Unity.Jobs.IJobFor");
119+
// public static readonly IClrTypeName JobParallelFor = new ClrTypeName("Unity.Jobs.IJobParallelFor");
120+
// public static readonly IClrTypeName AnimationJob = new ClrTypeName("UnityEngine.Animations.IAnimationJob");
121+
// public static readonly IClrTypeName JobParallelForTransform = new ClrTypeName("UnityEngine.Jobs.IJobParallelForTransform");
122+
// public static readonly IClrTypeName JobParticleSystem = new ClrTypeName("UnityEngine.ParticleSystemJobs.IJobParticleSystem");
123+
// public static readonly IClrTypeName JobParticleSystemParallelFor = new ClrTypeName("UnityEngine.ParticleSystemJobs.IJobParticleSystemParallelFor");
124+
// public static readonly IClrTypeName JobParticleSystemParallelForBatch = new ClrTypeName("UnityEngine.ParticleSystemJobs.IJobParticleSystemParallelForBatch");
120125
}
121-
}
126+
}

resharper/resharper-unity/src/Unity/UnityEditorIntegration/Api/UnityApi.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,11 @@ public UnityApi(UnityVersion unityVersion, UnityTypeCache unityTypeCache, UnityT
4747
myKnownTypesCache = knownTypesCache;
4848
}
4949

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

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

5856
// A serialised field cannot be abstract or generic, but a type declaration that will be serialised can be. This
5957
// method differentiates between a type declaration and a type usage. Consider renaming if we ever need to

resharper/resharper-unity/src/Unity/UnityEditorIntegration/Api/api.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
</parameters>
8484
<returns type="UnityEngine.Material" array="False" />
8585
</message>
86-
<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">
86+
<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">
8787
<parameters>
8888
<parameter type="System.String" array="True" name="importedAssets" />
8989
<parameter type="System.String" array="True" name="deletedAssets" />

resharper/resharper-unity/src/Unity/annotations/UnityEditor.CoreModule.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@
9595
</attribute>
9696
</member>
9797

98+
<!-- N:UnityEditor.Overlays -->
99+
<member name="T:UnityEditor.Overlays.OverlayAttribute">
100+
<attribute ctor="M:JetBrains.Annotations.MeansImplicitUseAttribute.#ctor" />
101+
<attribute ctor="M:JetBrains.Annotations.BaseTypeRequiredAttribute.#ctor(System.Type)">
102+
<argument>UnityEditor.Overlays.Overlay</argument>
103+
</attribute>
104+
</member>
105+
98106
<!-- N:UnityEditor.Rendering -->
99107
<member name="T:UnityEditor.Rendering.ScriptableRenderPipelineExtensionAttribute">
100108
<attribute ctor="M:JetBrains.Annotations.MeansImplicitUseAttribute.#ctor" />
@@ -107,4 +115,12 @@
107115
<member name="T:UnityEditor.ShortcutManagement.ClutchShortcutAttribute">
108116
<attribute ctor="M:JetBrains.Annotations.MeansImplicitUseAttribute.#ctor" />
109117
</member>
118+
119+
<!-- N:UnityEditor.Toolbars -->
120+
<member name="T:UnityEditor.Toolbars.EditorToolbarElementAttribute">
121+
<attribute ctor="M:JetBrains.Annotations.MeansImplicitUseAttribute.#ctor" />
122+
<attribute ctor="M:JetBrains.Annotations.BaseTypeRequiredAttribute.#ctor(System.Type)">
123+
<argument>UnityEngine.UIElements.VisualElement</argument>
124+
</attribute>
125+
</member>
110126
</assembly>

0 commit comments

Comments
 (0)