Coder Social home page Coder Social logo

Comments (7)

kyoh86 avatar kyoh86 commented on June 4, 2024 2

@Liooo @windard
Sorry for late.

I think I know the cause. But it is too difficult to be fixed by just me.

This linter is checking to see if the pointer of a loop variable is being brought into an external variable, but the check stops at where the "pointer is assigned to a variable".
If the destination of the assignment is an external variable, this linter can detect it, but as long as it assigns a local variable in the loop to its counterpart, it is assumed that there is no problem (negative).

A sample for positive:

var results []Result
for _, v := range iteratee {
   results = append(results, Result{v: &v})
}

results is an external variable. Assigning to it in one-statement, &v is found.

exportloopref does not track how the value of that local variable is subsequently handled.
In this case, the value placed in the local variable is then assigned (as a result of being appended) to the external variable, which should be detected as a problem.

However, tracking the subsequent treatment of the local variable requires recursively reanalyzing ast once analyzed, which is somewhat expensive and creates the need to track all assignments.
Currently, it is possible to analyze the ast in 1-pass, and if multiple analyses are required, such as 2-pass and 3-pass, the process of this linter will become heavier and heavier, so for my part, it is difficult to judge whether it is feasible or not.
Of course, it is possible to cover n assignments at most, as in the construction of struct, but I do not have a basis for determining a good upper limit.

CONCLUSIONS:

  • If anyone knows how to track such assignments in 1-pass, a Pull-Request would be greatly appreciated.
  • If anyone can analyze what kind of performance problems 2-pass, 3-pass, or n-pass would cause, please provide a good upper bound with benchmarks so I can adopt it.

from exportloopref.

windard avatar windard commented on June 4, 2024 1

Even public fields are not detected.

    type Result struct {
        V *string
    }
    var results []Result

    for _, v := range []string{"a", "b", "c"} {
        r := Result{V: &v}
        results = append(results, r)
    }

from exportloopref.

windard avatar windard commented on June 4, 2024

@kyoh86 plz take a look.

from exportloopref.

kyoh86 avatar kyoh86 commented on June 4, 2024

okay, I'll check it out.

from exportloopref.

Me1onRind avatar Me1onRind commented on June 4, 2024

How about provide a strict mode that not allow use any loop var ref. It's simple and easy although violence and rugged.

from exportloopref.

kyoh86 avatar kyoh86 commented on June 4, 2024

@Me1onRind If you want it, you can use looppointer instead.

from exportloopref.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.