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.
0
Avatar
Permanently deleted user

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
Avatar
Permanently deleted user

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

0
Avatar
Permanently deleted user

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

0
Avatar
Permanently deleted user

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
Avatar
Permanently deleted user

Hi Mark.

That works a treat!

Thanks!

0

请先登录再写评论。