News for hackers. Stuff that matters.
Re:Semgrep: Semantic grep for code by enriquto on Thursday April 22, 2021 @01:24PM (#26905333)
> You need to enable JavaScript to run this app.

Wait, is this a web app? I was expecting a command line tool to navigate my code locally.

Re:Semgrep: Semantic grep for code by thomasahle on Thursday April 22, 2021 @01:26PM (#26905366)
The cli is here: https://semgrep.dev/docs/getting-started/

You can write stuff like

    # Check for Python == where the left and right hand sides are the same (often a bug)
    $ semgrep -e '$X == $X' --lang=py path/to/src
Re:Semgrep: Semantic grep for code by enriquto on Thursday April 22, 2021 @01:37PM (#26905507)
Cool! But this example is a bit simplistic since it can be done just as easily by regular grep:

    grep -E '(.+) = \1' *.py
I have trouble looking at the examples in the project website (many things inside iframes are adblocked). Do you have any example of a search that would be difficult or impossible with grep?
Re:Semgrep: Semantic grep for code by fragmede on Thursday April 22, 2021 @01:45PM (#26905611)
If you're that capable with grep, surely you've run into it's shortcomings and can appreciate attempts to improve the status quo. Like trying to find usages of a variable that is a substring of a word. Like trying to fix code where somebody else has used the variable name i. Sure, you can add matching on word boundaries, but then it starts to become a distraction from the original task at hand.
Re:Semgrep: Semantic grep for code by mdaniel on Thursday April 22, 2021 @10:16PM (#26910350)
Your example matches the _text_, semgrep matches _the language_

given:

    if 1 \
        == \
        1:
        pass
then

    $ semgrep -e '$X == $X' -l python
    1:if 1 \
    2:        == \
    3:        1:
    ran 1 rules on 1 files: 1 findings
to follow myself up, one can also check for expressions like what I used as an example and say "don't do that", but without regard to what is inside the if

    semgrep -e $'if ...:\n  pass\n' -l python
Re:Semgrep: Semantic grep for code by SavantIdiot on Thursday April 22, 2021 @01:40PM (#26905537)
It can infer (x==y) if x=1 and y=1, which is grep cannot do.
Re:Semgrep: Semantic grep for code by enriquto on Thursday April 22, 2021 @01:43PM (#26905579)
This must surely fail... isn't it equivalent to solving the halting problem? Or does it run the whole program like a debugger?
Re:Semgrep: Semantic grep for code by Spivak on Thursday April 22, 2021 @02:08PM (#26905900)
It fails pretty fast.

    import random

    y = 0

    def f(x):
        print(x)

    if random.randint(0,1) == 2:
        y = 1

    f(y)
This not only fails but crashes the program.
Re:Semgrep: Semantic grep for code by Quekid5 on Thursday April 22, 2021 @01:58PM (#26905772)
If false negatives are permitted the halting problem isn't an issue.
Re:Semgrep: Semantic grep for code by exdsq on Thursday April 22, 2021 @01:26PM (#26905360)
There’s a demo on the site, I assume that’s it?
Re:Semgrep: Semantic grep for code by enriquto on Thursday April 22, 2021 @01:28PM (#26905392)
No, it's just the landing page. Apparently it does not allow to see it without running some javascript.
Re:Semgrep: Semantic grep for code by sahkopoyta on Thursday April 22, 2021 @04:35PM (#26907855)
Well the demo is there on the landing page