Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CodeQL issues #285

Merged
merged 1 commit into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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>();

Check notice

Code scanning / CodeQL

Static field written by instance method

Write to static field from instance method or constructor.
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 { } }

Check warning

Code scanning / CodeQL

Property value is not used when setting a property

Value ignored when setting property.
}

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