Skip to content

Commit bbe8199

Browse files
authored
Fix CodeQL issues (#285)
1 parent 688a0f1 commit bbe8199

File tree

4 files changed

+17
-22
lines changed

4 files changed

+17
-22
lines changed

src/PublicApiGenerator/CecilEx.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -167,17 +167,14 @@ private static IEnumerable<TypeDefinition> GetBaseTypes(TypeDefinition type)
167167

168168
private static CodeTypeReference ModifyCodeTypeReference(CodeTypeReference typeReference, string modifier)
169169
{
170-
using (var provider = new CSharpCodeProvider())
171-
{
172-
if (typeReference.TypeArguments.Count == 0)
173-
// For types without generic arguments we resolve the output type directly to turn System.String into string
174-
return new CodeTypeReference(modifier + " " + provider.GetTypeOutput(typeReference));
175-
else
176-
// For types with generic types the BaseType is GenericType`<Arity>. Then we cannot resolve the output type and need to pass on the BaseType
177-
// to avoid falling into hardcoded assumptions in CodeTypeReference that cuts of the type after the 4th comma. i.ex. readonly Func<string, string, string, string>
178-
// works but readonly Func<string, string, string, string, string> would turn into readonly Func<string
179-
return new CodeTypeReference(modifier + " " + typeReference.BaseType, typeReference.TypeArguments.Cast<CodeTypeReference>().ToArray());
180-
}
170+
using var provider = new CSharpCodeProvider();
171+
return typeReference.TypeArguments.Count == 0
172+
// For types without generic arguments we resolve the output type directly to turn System.String into string
173+
? new CodeTypeReference(modifier + " " + provider.GetTypeOutput(typeReference))
174+
// For types with generic types the BaseType is GenericType`<Arity>. Then we cannot resolve the output type and need to pass on the BaseType
175+
// to avoid falling into hardcoded assumptions in CodeTypeReference that cuts of the type after the 4th comma. i.ex. readonly Func<string, string, string, string>
176+
// works but readonly Func<string, string, string, string, string> would turn into readonly Func<string
177+
: new CodeTypeReference(modifier + " " + typeReference.BaseType, typeReference.TypeArguments.Cast<CodeTypeReference>().ToArray());
181178
}
182179

183180
internal static bool? IsNew<TDefinition>(this TDefinition methodDefinition, Func<TypeDefinition, Collection<TDefinition>?> selector, Func<TDefinition, bool> predicate)

src/PublicApiGenerator/CodeTypeReferenceBuilder.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ private static CodeTypeReference CreateCodeTypeReferenceWithNullabilityMap(TypeR
1818
if (type.IsValueType && type.Name == "Nullable`1" && type.Namespace == "System")
1919
{
2020
// unwrap System.Nullable<Type> into Type? for readability
21-
var genericArgs = type is IGenericInstance instance ? instance.GenericArguments : type.HasGenericParameters ? type.GenericParameters.Cast<TypeReference>() : null;
21+
var genericArgs = type is IGenericInstance instance
22+
? instance.GenericArguments
23+
: type.HasGenericParameters ? type.GenericParameters.Cast<TypeReference>() : throw new NotSupportedException(type.ToString());
2224
return CreateCodeTypeReferenceWithNullabilityMap(genericArgs.Single(), nullabilityMap, NullableMode.Force, disableNested);
2325
}
2426
else

src/PublicApiGenerator/NullableContext.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ private static Stack<ICustomAttributeProvider> NullableContextProviders
1212
{
1313
get
1414
{
15-
if (_nullableContextProviders == null)
16-
_nullableContextProviders = new Stack<ICustomAttributeProvider>();
15+
_nullableContextProviders ??= new Stack<ICustomAttributeProvider>();
1716
return _nullableContextProviders;
1817
}
1918
}

src/PublicApiGeneratorTests/Property_methods.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,12 @@ namespace Examples
139139
{
140140
public class PropertyReadWrite
141141
{
142-
public string Value { get { return string.Empty; } set { } }
142+
public string Value { get => string.Empty; set { } }
143143
}
144144

145145
public class PropertyReadOnly
146146
{
147-
public string Value { get { return string.Empty; } }
147+
public string Value => string.Empty;
148148
}
149149

150150
public class PropertyWriteOnly
@@ -166,17 +166,14 @@ public class PropertyIndexer
166166
{
167167
public string this[int index]
168168
{
169-
get { return string.Empty; }
169+
get => string.Empty;
170170
set { }
171171
}
172172
}
173173

174174
public class PropertyIndexerReadOnly
175175
{
176-
public string this[int index]
177-
{
178-
get { return string.Empty; }
179-
}
176+
public string this[int index] => string.Empty;
180177
}
181178

182179
public class PropertyIndexerWriteOnly
@@ -191,7 +188,7 @@ public class PropertyIndexerMultipleParameters
191188
{
192189
public string this[int index, int order]
193190
{
194-
get { return string.Empty; }
191+
get => string.Empty;
195192
set { }
196193
}
197194
}

0 commit comments

Comments
 (0)