In the US, the legal doctrine of Fair Use does not apply to embedding excerpts of copyrighted works into source code. How such doctrine is applied by the laws of your jurisdiction may vary.
I will excerpt from a US Copyright Office article on Fair Usearticle on Fair Use, and of copyrights generally:
Section 107 contains a list of the various purposes for which the reproduction of a particular work may be considered fair, such as criticism, comment, news reporting, teaching, scholarship, and research [emphasis mine]. Section 107 also sets out four factors to be considered in determining whether or not a particular use is fair:
- The purpose and character of the use, including whether such use is of commercial nature or is for nonprofit educational purposes
- The nature of the copyrighted work
- The amount and substantiality of the portion used in relation to the copyrighted work as a whole
- The effect of the use upon the potential market for, or value of, the copyrighted work.
The distinction between fair use and infringement may be unclear and not easily defined. There is no specific number of words, lines, or notes [emphasis mine] that may safely be taken without permission. Acknowledging the source of the copyrighted material does not substitute for obtaining permission.
It summarizes the legal limits of copyright, which Fair Use further limits, like so:
Copyright protects the particular way an author has expressed himself. It does not extend to any ideas, systems, or factual information conveyed in the work.
The safest course is always to get permission from the copyright owner before using copyrighted material. The Copyright Office cannot give this permission.
What the limits of what copyright protects tells us is though you cannot copy "snippets" of code via Fair Use, you can rewrite the way ideas, systems (including algorithms), or factual information are expressed in those snippets.
In short, 3 lines of code should be small enough for you to rewrite it so as not to violate the original work's copyright. It probably took longer to write your question than it will to do the rewrite.