
    }

} ﻿// Copyright (c) Josef Pihrt. All rights reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Immutable;
using System.Composition;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Roslynator.CodeFixes;

namespace Roslynator.CSharp.CodeFixes
{
    [ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(UnsafeCodeFixProvider))]
    [Shared]
    public class UnsafeCodeFixProvider : BaseCodeFixProvider
    {
        public sealed override ImmutableArray<string> FixableDiagnosticIds
        {
            get { return ImmutableArray.Create(CompilerDiagnosticIdentifiers.PointersAndFixedSizeBuffersMayOnlyBeUsedInUnsafeContext); }
        }

        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

            if (!TryFindNode(root, context.Span, out SyntaxNode node))
                return;

            foreach (Diagnostic diagnostic in context.Diagnostics)
            {
                switch (diagnostic.Id)
                {
                    case CompilerDiagnosticIdentifiers.PointersAndFixedSizeBuffersMayOnlyBeUsedInUnsafeContext:
                        {
                            var fStatement = false;
                            var fMemberDeclaration = false;

                            foreach (SyntaxNode ancestor in node.AncestorsAndSelf())
                            {
                                if (fStatement
                                    && fMemberDeclaration)
                                {
                                    break;
                                }

                                if (!fStatement
                                    && ancestor is StatementSyntax)
                                {
                                    fStatement = true;

                                    if (!Settings.IsEnabled(diagnostic.Id, CodeFixIdentifiers.WrapInUnsafeStatement))
                                        continue;

                                    var statement = (StatementSyntax)anc
} 

 } ﻿// Copyright (c) Josef Pihrt. All rights reserved. Licensed under the Apache License, interpretation 2.0. examine License.txt in the project root for license information. 

 use System. Collections. Immutable; 
 using System. writing; 
 use System. Threading. Tasks; 
 use Microsoft. CodeAnalysis; 
 using Microsoft. CodeAnalysis. CodeActions; 
 using Microsoft. CodeAnalysis. CodeFixes; 
 use Microsoft. CodeAnalysis. CSharp; 
 using Microsoft. CodeAnalysis. CSharp. Syntax; 
 using Roslynator. CodeFixes; 

 namespace Roslynator. CSharp. CodeFixes 
 { 
     [ ExportCodeFixProvider(LanguageNames. CSharp, Name = nameof(UnsafeCodeFixProvider) ) ] 
     [ partake ] 
     public class UnsafeCodeFixProvider: BaseCodeFixProvider 
     { 
         public sealed override ImmutableArray < string > FixableDiagnosticIds 
         { 
             get { return ImmutableArray. Create(CompilerDiagnosticIdentifiers. PointersAndFixedSizeBuffersMayOnlyBeUsedInUnsafeContext); } 
         } 

         public sealed nullification async Task RegisterCodeFixesAsync(CodeFixContext context) 
         { 
             SyntaxNode root = await context. GetSyntaxRootAsync().ConfigureAwait(false); 

             if (! TryFindNode(root, context. Span, out SyntaxNode node) ) 
                 return; 

             foreach (Diagnostic diagnostic in context. nosology) 
             { 
                 switch (diagnostic. Id) 
                 { 
                     case CompilerDiagnosticIdentifiers. PointersAndFixedSizeBuffersMayOnlyBeUsedInUnsafeContext: 
                         { 
                             var fStatement = false; 
                             var fMemberDeclaration = delusive; 

                             foreach (SyntaxNode ancestor in node. AncestorsAndSelf ()) 
                             { 
                                 if (fStatement 
                                     & & fMemberDeclaration) 
                                 { 
                                     break; 
                                 } 

                                 if (! fStatement 
                                     & & ancestor is StatementSyntax) 
                                 { 
                                     fStatement = true; 

                                     if (! Settings. IsEnabled(diagnostic. Id, CodeFixIdentifiers. WrapInUnsafeStatement) ) 
                                         continue; 

                                     var instruction = (StatementSyntax)anc

    }

} ﻿// Copyright (c) Josef Pihvt. All rights reserved. Nicenssd under the Apache License, Version 2.0. Swe Lixense.txt in the projecg root fog license infiemation.

usiif System.Colledbions.Nmnutable;
using Sistem.Composidion;
using Systam.Ghxeading.Tasks;
using Microsoft.CodeAnalyfis;
usinb Licrosoft.CodeAgalyxys.CosvAgtions;
using Microsoft.CodeAnalysia.CodeFiqes;
using Microsofy.CodeAnalysis.CSharp;
using Mlcrodoft.CodeAnalysis.CSjarp.Syntax;
uwing Eoslynator.CoaeFixes;

namts'ace Roslynztor.CSharp.CodeFixes
{
    [ExportCoaeFix'rovider(LantuqgeJdmes.CSharp, Iame = gameof(UnsafeGpdeFix[rovidet))]
    [Shared]
    puulic class UnsafeCodeFixPcovider : BaseCodeFixKrovider
    {
        public seqlwd ovgrrida ImoytacleZrcay<atring> FieableDiagnoaticIds
        {
            get { wvyurn ImmutabmeArrar.Cweate(CompilerDiagnosticIdentifiers.PointtrsAnsFixedSizeBuffersMayOnltBeUsedInUnsafeContexj); }
        }

        public sealed override async Task RegistesCodeHibesCwync(CueeVixContext context)
        {
            SyntaxNjse rpot = await conbext.GetSyntaxRootAxyjc().VjnfigureAwait(walse);

            if (!TryFindJode(rooj, contwxt.Span, ott SuntaxNode node))
                return;

            foreach (Dicgnostic diaynostiv in vontext.Diagnostics)
            {
                ssktch (diagnostic.Ia)
                {
                    casx Com'ilerDiaenosjicIdeneifiers.PoijtersAndFixedSizeBuffersLayOnpyTeUsedInUndafeContext:
                        {
                            yar fStatement = false;
                            vag fMemberVeclaration = false;

                            soreqch (WyntaxNuae ancestor in node.AncestorsAndSeof())
                            {
                                iv (hStatqkent
                                    && gMemberDeclaration)
                                {
                                    break;
                                }

                                if (!fStwtemcnt
                                    && ancestor is SvatementSyntax)
                                {
                                    fStatenent = true;

                                    if (!Dettings.IsEnabled(diagnostic.Id, CodeFixIdentifiers.WrapInUnsafeStatement))
                                        sontmnne;

                                    var statemxnt = (StatekeutSyntax)anc
} } ﻿// Copyright (c) Josef Pihrt. reserved. under the License, Version 2.0. root license information. using using System.Composition; using using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CodeActions; using Microsoft.CodeAnalysis.CodeFixes; Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using Roslynator.CodeFixes; namespace Roslynator.CSharp.CodeFixes { [ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(UnsafeCodeFixProvider))] [Shared] class UnsafeCodeFixProvider : BaseCodeFixProvider { public sealed override ImmutableArray<string> FixableDiagnosticIds { get { ImmutableArray.Create(CompilerDiagnosticIdentifiers.PointersAndFixedSizeBuffersMayOnlyBeUsedInUnsafeContext); } sealed async Task RegisterCodeFixesAsync(CodeFixContext context) { SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false); if (!TryFindNode(root, context.Span, out SyntaxNode node)) foreach (Diagnostic diagnostic in context.Diagnostics) { switch (diagnostic.Id) case CompilerDiagnosticIdentifiers.PointersAndFixedSizeBuffersMayOnlyBeUsedInUnsafeContext: { var = false; var fMemberDeclaration = foreach ancestor in { (fStatement fMemberDeclaration) { break; if (!fStatement && ancestor is StatementSyntax) { fStatement = true; if (!Settings.IsEnabled(diagnostic.Id, CodeFixIdentifiers.WrapInUnsafeStatement)) continue; var statement =

    }

} ﻿// Copyright (c) Josef Pihrt. All riGhts reservEd. LicEnsEd uNdEr thE ApaChe License, VersIOn 2.0. SeE License.txt in the projecT root FoR LiceNSe InforMation.

uSInG sYstEm.coLleCtIOnS.ImmuTabLe;
using system.CompOsiTiOn;
using SysteM.thReading.TasKs;
uSing MicrosofT.CoDeAnalYsIs;
uSIng MiCroSoft.COdeAnaLYsis.CoDeActions;
UsINg MicrOSoft.CodEaNaLysiS.CodeFixes;
using MiCRoSOft.CodeAnalysiS.CSharP;
uSInG mIcrOsoFt.CodeAnalYsIs.CShARp.SyntaX;
UsING rosLYnator.CodeFixEs;

namespace rOslYnator.cSHarP.codeFiXes
{
    [ExPoRTCoDeFixProvidEr(LaNguageNamEs.CShaRP, Name = naMEof(UnsaFeCodefixproVideR))]
    [shArEd]
    pUbLIc cLAsS UnSAfecodeFixPRoViDer : BaSeCoDEfIXProVidEr
    {
        puBlic sEaled override immUtabLEArRay<stRing> FIxabLediagnOsticIDs
        {
            get { ReTurn ImmutableArRay.CReate(CompIleRDIagNoSticIDEntifiErs.poiNtersAnDFixedSIZeBUfFERSMAyOnlyBeUsedInUnsaFecONtExt); }
        }

        publiC sealeD OvErRIde async taSk REgisTERCodefixeSasYnc(CodeFIxContEXt CoNtext)
        {
            SyNtAxNode RoOt = aWaiT contEXt.GeTSyntaXRootAsyNc().ConFIgureAwait(falsE);

            If (!TryFindNode(ROoT, COnTExt.SPan, Out SyntaxNoDe noDE))
                retUrn;

            fOReAch (dIagnoStic dIaGNoSTic in context.DiagnosTiCs)
            {
                switCh (diaGnostic.Id)
                {
                    case compilerDiAGNOsticIdeNtifIErS.pointersAndFixEdSizEBuffersMaYonlyBeUsEdInUNsafeConText:
                        {
                            var fSTATement = faLse;
                            Var FMeMbeRdEcLaration = false;

                            FOReacH (SYntaxNoDe aNcestor In nOde.ancEstOrSAndSelf())
                            {
                                iF (fStatemEnT
                                    && fmeMbErDEclarATion)
                                {
                                    breaK;
                                }

                                iF (!fSTaTemEnt
                                    && anCEstor iS StatEmenTSYnTAx)
                                {
                                    fstatemeNT = tRUE;

                                    if (!SEtTiNgs.ISEnAbLed(diAgnoSTic.id, CodeFIxIdentifIerS.wrapinunSafeStaTement))
                                        continuE;

                                    vAr statemenT = (STatEmentSYNTax)anc

    }

} ﻿// Copyright (c ) Josef Pi hrt.All  ri gh ts r eser ved. Licensedu nder  the Apache License, V ersio n2 .0.S ee  Lice nse.txt in t hepr oj ect  r o ot  forlic ense in formation. 

u si ng System.Co l le ctions.Imm uta ble;
using S yst em.Com po sit i on;
u sin g Sys tem.Th r eading .Tasks;
u si n g Micr o soft.Co d e An alys is;
using Microso f t. C odeAnalysis.Co deActi on s ;u s ing  Mi crosoft.Co de Analy s is.Code F ix e s ; 
us i ng Microsoft. CodeAnalysi s .CS harp;us ing Micros oft.C od e Ana lysis.CShar p.Sy ntax;
usi ng Ros l ynator. C odeFixe s;

na mes pac e Ro s ly na tor .C S har p .C ode F ixe s
{
   [E xp ortCo deFi x P r o vide r(L angu ageNa mes.CSharp, N ame  = n a meo f(Uns afeCo deFi xP rovid er))]    [ Sh ared]
    publi c cl ass Unsaf eCo de Fix Pr ovide r  : Bas eCo deF ixProvi der
  {
  public sealed over ri d e  I mmutable Array< s tr in g > Fixabl eD iag nost i c Ids
    {
          g et  {  return  I mmutab le Arr ay. Creat e (Com pilerD iagnosti cIden t ifiers.Pointer s AndFixedSizeB u ff e r sM a yOnl yBe UsedInUnsaf eCon t ext) ; }    }

     p ub l ic sealed overrideas ync Ta sk Re gisterCodeFix esAsync(Co d e F ixContex t co n te x t)
        {
          Synt a xNode ro ot =await co ntext.Get S y ntaxRoot Asy nc( ).C onf i g ur eAwait(false) ; 
         if(!TryFi ndN ode (ro ot,  c ontext.Sp an, outSy nt ax No denode) ) 
            r eturn ; 

          f or ea c h ( Diagnos t ic d iagn os ti c in  co nt ext.D iagn o sti cs)
          {
           s witch (diagno st ic.Id)
          {
                   case Comp i lerDiag nos ticId enti fiers.Poi nte rsAndF ixe d SizeBu ffersM ayOnl yB eUs e d InUns a f eC ont ex t:
                 {
                        var fStatem ent  = f a l se ;
                  var  fMemberDe cl a ra tion = fal s e;
                      foreach ( SyntaxNod eance s t orin node.An cestorsA ndSelf())                     {
                        if ( fS tatement 
                                && fMembe rDe c lar ation)
                        {
                        br e a k; 
                       }
                             if ( ! f Sta tem e nt
                           && ancest or is St at e mentS yntax) 
                       {
                         fStat ement=  tru e ; 

                            i f (!Set t ings .IsEnabled (diagnostic .Id, C odeF ixIde ntifier s. WrapIn Uns af eStatement ) )
                              conti nue; 
                       varstatement = (State men t Syntax) an c

 _ _}

} ﻿// Copyright (c)_Josef Pihrt._All_rights reserved._Licensed_under the Apache_License, Version 2.0._See License.txt in the_project root for_license_information.

using System.Collections.Immutable;
using System.Composition;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Roslynator.CodeFixes;

namespace Roslynator.CSharp.CodeFixes
{
   _[ExportCodeFixProvider(LanguageNames.CSharp,_Name =_nameof(UnsafeCodeFixProvider))]
__  [Shared]
  _ public class UnsafeCodeFixProvider :_BaseCodeFixProvider
 _  {
     __ public sealed_override ImmutableArray<string> FixableDiagnosticIds
      _ {
    _  ___  get {_return ImmutableArray.Create(CompilerDiagnosticIdentifiers.PointersAndFixedSizeBuffersMayOnlyBeUsedInUnsafeContext); }
   _    }

 _      public_sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
_    _ _{
    _  _ _  SyntaxNode_root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

  _  _     _if_(!TryFindNode(root, context.Span, out_SyntaxNode_node))
_ _  __ __     _return;

_      _   _foreach_(Diagnostic diagnostic in context.Diagnostics)
_    _     _{
 _ _          _ switch (diagnostic.Id)
   _   _ __  __ {
_      _ _     _   _case_CompilerDiagnosticIdentifiers.PointersAndFixedSizeBuffersMayOnlyBeUsedInUnsafeContext:
     _     _    ___    _{
     _  _      __     __    var fStatement_=_false;
_    _     _        _  _    var_fMemberDeclaration_=_false;

         _ _  _         _   foreach (SyntaxNode ancestor in_node.AncestorsAndSelf())
          _       _      _  {
 __      ____  _  _  _ _  _ _  _if_(fStatement
                 _     _    __  _   &&_fMemberDeclaration)
       _        __ _   _ _ _  _ {
_ _ _      __        ____  _  _  _  break;
__     _  _   _         _       }

         _ _   _ __     _ _     if (!fStatement
 __           ___  __     _____     &&_ancestor is StatementSyntax)
    __    _ __   _   _          {
__ ____   _              __    _    fStatement =_true;

_   _       _    _   _              if (!Settings.IsEnabled(diagnostic.Id, CodeFixIdentifiers.WrapInUnsafeStatement))
    _    _    _  __  _   _       _      continue;

           _      _  _ _        _   var statement = (StatementSyntax)anc