Skip to content

Commit

Permalink
Fix CodeQL issues (#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
sungam3r committed Feb 27, 2023
1 parent 688a0f1 commit bbe8199
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 22 deletions.
19 changes: 8 additions & 11 deletions src/PublicApiGenerator/CecilEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,14 @@ private static IEnumerable<TypeDefinition> GetBaseTypes(TypeDefinition type)

private static CodeTypeReference ModifyCodeTypeReference(CodeTypeReference typeReference, string modifier)
{
using (var provider = new CSharpCodeProvider())
{
if (typeReference.TypeArguments.Count == 0)
// For types without generic arguments we resolve the output type directly to turn System.String into string
return new CodeTypeReference(modifier + " " + provider.GetTypeOutput(typeReference));
else
// For types with generic types the BaseType is GenericType`<Arity>. Then we cannot resolve the output type and need to pass on the BaseType
// 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>
// works but readonly Func<string, string, string, string, string> would turn into readonly Func<string
return new CodeTypeReference(modifier + " " + typeReference.BaseType, typeReference.TypeArguments.Cast<CodeTypeReference>().ToArray());
}
using var provider = new CSharpCodeProvider();
return typeReference.TypeArguments.Count == 0
// For types without generic arguments we resolve the output type directly to turn System.String into string
? new CodeTypeReference(modifier + " " + provider.GetTypeOutput(typeReference))
// For types with generic types the BaseType is GenericType`<Arity>. Then we cannot resolve the output type and need to pass on the BaseType
// 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>
// works but readonly Func<string, string, string, string, string> would turn into readonly Func<string
: new CodeTypeReference(modifier + " " + typeReference.BaseType, typeReference.TypeArguments.Cast<CodeTypeReference>().ToArray());
}

internal static bool? IsNew<TDefinition>(this TDefinition methodDefinition, Func<TypeDefinition, Collection<TDefinition>?> selector, Func<TDefinition, bool> predicate)
Expand Down
4 changes: 3 additions & 1 deletion src/PublicApiGenerator/CodeTypeReferenceBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ private static CodeTypeReference CreateCodeTypeReferenceWithNullabilityMap(TypeR
if (type.IsValueType && type.Name == "Nullable`1" && type.Namespace == "System")
{
// unwrap System.Nullable<Type> into Type? for readability
var genericArgs = type is IGenericInstance instance ? instance.GenericArguments : type.HasGenericParameters ? type.GenericParameters.Cast<TypeReference>() : null;
var genericArgs = type is IGenericInstance instance
? instance.GenericArguments
: type.HasGenericParameters ? type.GenericParameters.Cast<TypeReference>() : throw new NotSupportedException(type.ToString());
return CreateCodeTypeReferenceWithNullabilityMap(genericArgs.Single(), nullabilityMap, NullableMode.Force, disableNested);
}
else
Expand Down
3 changes: 1 addition & 2 deletions src/PublicApiGenerator/NullableContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ private static Stack<ICustomAttributeProvider> NullableContextProviders
{
get
{
if (_nullableContextProviders == null)
_nullableContextProviders = new Stack<ICustomAttributeProvider>();
_nullableContextProviders ??= new Stack<ICustomAttributeProvider>();
return _nullableContextProviders;
}
}
Expand Down
13 changes: 5 additions & 8 deletions src/PublicApiGeneratorTests/Property_methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,12 @@ namespace Examples
{
public class PropertyReadWrite
{
public string Value { get { return string.Empty; } set { } }
public string Value { get => string.Empty; set { } }
}

public class PropertyReadOnly
{
public string Value { get { return string.Empty; } }
public string Value => string.Empty;
}

public class PropertyWriteOnly
Expand All @@ -166,17 +166,14 @@ public class PropertyIndexer
{
public string this[int index]
{
get { return string.Empty; }
get => string.Empty;
set { }
}
}

public class PropertyIndexerReadOnly
{
public string this[int index]
{
get { return string.Empty; }
}
public string this[int index] => string.Empty;
}

public class PropertyIndexerWriteOnly
Expand All @@ -191,7 +188,7 @@ public class PropertyIndexerMultipleParameters
{
public string this[int index, int order]
{
get { return string.Empty; }
get => string.Empty;
set { }
}
}
Expand Down

0 comments on commit bbe8199

Please sign in to comment.