Does the syntax coloring I see indicate a real problem?

I need a behaviour provided by:

x = 2;
Block[{x = HoldForm[x]}, x]

x


FrontEnd marks this syntax as not quite correct.

It has not caused me any troubles so far so my question is: could it be a problem?

-
I don't think it's a problem; the front end produces this coloring as a hint to the user that there could be a scoping inconsistency, not to say that the code is necessarily erroneous. The greater question in my mind is why does the FE apply the coloring in this particular case, which to me looks rather benign. (For others' reference, the FE is saying that x will go out of scope before being used. I must admit I don't understand what this even means for HoldForm.) –  Oleksandr R. May 23 '14 at 23:55
The notebook code editor is far from perfect. It produces strange coloration in many other situations. –  m_goldberg May 24 '14 at 2:26

1 Answer

The FE only looks at the structure of your code for colouring. It doesn't evaluate anything. This means two things: (1) it can only guess that there might be a problem, because by looking at the structure, it doesn't know whether your code really evaluates to something you might not want. (2) You can easily trick the FE by changing the structure into something equivalent which looks different. With this you can easily trick the FE into whatever colouring you prefer. Here are only some examples of exactly the same code:

-
Or: Block[{x=HoldForm@x},x] –  Rolf Mertig May 24 '14 at 6:27
@RolfMertig Included! –  halirutan May 24 '14 at 8:28
@halirutan So do you think it is only artifact/preventive behaviour and not an idication of any subtle cases that I may face one day? –  Kuba May 24 '14 at 8:36
There is one issue: It seems you cannot remove the HoldForm wrapper inside the Block. If you use x=HoldForm[a] instead, everything works as expected. Look at the output of this and then replace a by x. –  halirutan May 25 '14 at 1:28
I stumbled over this when I was trying to mess with the scoping by releasing the hold of x. I wanted to see what happens because the x inside the HoldForm is the global x. Obviously they prevented it. What consequences this has for your code, I don't know. –  halirutan May 25 '14 at 1:30