SS For non-braced if statements

I would like to search for all if statements that have no braces:

if(condition)
     //somehting
else
     //something

and convert them to if statements with braces:

if(condition)
{
    //somehting
}
else
{
     //something
}

I have tried the below SS but it matches both braced and non-braced if statements:

if ($Condition$)
     $ThenStatement$;
else
     $ElseStatement$;


Is this possible with SS?

Thanks in advance.
5 comments

dunno about Structural Search, but you can use code reformatting and it will add it for you..(you need to set codeformatting to always add brackets around if/else statements)

0

I cant reformat the code - it will make subsequent merges really painful.

0

Anyone know how to this? Any help would be greatly appreciated.

0

Is using the built in code inspection that does this not an option for you? Create an inspection profile with the inspection "Code Style issues > Control flow statements without braces" selected. (it can be a profile with just that inspection if you want.)  Run the inspection profile via Analyze > Inspect Code. (You can limit the scope of what files are inspected if desired.) In the Inspection tool window, find the "Control flow statements without braces" item in the results and select it. Then do an "apply fix" for that inspection (either via the right-click context menu or the light bulb icon on the toolbar.) That will fix all the missing brace issues (for the scope you ran the analysis on) without affecting other code as the reformatting option suggested above would.

It seems like writing a SSR for this would get complex.

0

Hi Mark.

That works a treat!

Thanks!

0

Please sign in to leave a comment.