Skip to content

Instantly share code, notes, and snippets.

View controlflow's full-sized avatar

Alexander Shvedov controlflow

View GitHub Profile
public override bool Match(ITreeNode element, IMatchingContext context)
{
if (element == null)
return false;
if (element is ICSharpArgument arg)
element = arg.Value;
if (element is IExpressionInitializer exprInitializer)
element = exprInitializer.Value;
var s = x as string;
if (s == null) {
s = DefaultValue();
}
Console.WriteLine(s);
// ==>
if (!(x is string s)) {
using System;
interface ITreeNode { }
interface IScope { }
class C
{
public bool InteriorShouldBeProcessed(ITreeNode element)
{
return !(element is IScope) || ScopeShouldBeVisited((IScope) element);
[Pure, ContractAnnotation("null => null")]
public static ICSharpTreeNode GetCodeBody([CanBeNull] this ICSharpDeclaration declaration)
{
if (declaration == null) return null;
var functionDeclaration = declaration as ICSharpFunctionDeclaration;
if (functionDeclaration != null && functionDeclaration.Body != null)
{
return functionDeclaration.Body;
}
@controlflow
controlflow / after.cs
Created November 10, 2016 00:32
switch2.cs
private void RenderCaseLabel(
[NotNull] ICSharpExpression conditionExpression, [NotNull] ISwitchCaseLabel switchCaseLabel,
ref FactoryArgumentsBuilder builder, bool expressionIsPartOfDisjunction)
{
var guardClause = switchCaseLabel.Guard;
var emitParentheses = expressionIsPartOfDisjunction || (guardClause != null && guardClause.Condition != null);
if (emitParentheses) builder.Append('(');
switch (switchCaseLabel.Pattern)
ITreeNode holdingNode = holder is ICSharpFile ? (ITreeNode) holder : ((ICSharpNamespaceDeclaration) holder).Body;
// good old if-then-throw
if (foo == null)
throw new ArgumentNullException("foo");
// nameof-flavored if-then-throw
if (foo == null)
throw new ArgumentNullException(nameof(foo));
// reference equality to avoid operator== overload
if (ReferenceEquals(foo, null))
@controlflow
controlflow / a.cs
Last active November 28, 2016 21:16
[Pure]
private static bool IsNegativeInfinity([NotNull] ICSharpExpression expression)
{
var constantValue = expression.ConstantValue.Value;
if (constantValue is double)
return double.IsNegativeInfinity((double) constantValue);
if (constantValue is float)
return float.IsNegativeInfinity((float) constantValue);
return false;
{
"System.Collections.Generic": {
"IReadOnlyList`2": {
"P:Keys": "NotNull",
"P:Values": "NotNull",
"M:TryGetValue(`0,`1@)": [
"Pure",
["ContractAnnotation", "=> true; => false, value: null"],
{ "key": "NotNull" }
],
// C# specs paragraph 18.4
if (from is IPointerType && to is IPointerType)
{
IType toElementType = ((IPointerType)to).ElementType;
return predefinedType.Void.Equals(toElementType) ||
((IPointerType)from).ElementType.Equals(toElementType);
}