Skip to content

Conversation

@webwarrior-ws
Copy link
Contributor

Add FavourNestedFunctions rule.

Fixes #638

@webwarrior-ws webwarrior-ws force-pushed the favour-nested-functions branch from 2857119 to 8d130ab Compare December 21, 2023 14:11
@knocte knocte changed the title Favour nested functions DRAFT: Favour nested functions Jan 10, 2024
Copy link
Collaborator

@xperiandri xperiandri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, just a few remarks

@xperiandri
Copy link
Collaborator

Could you rebase and fix?

@webwarrior-ws webwarrior-ws force-pushed the favour-nested-functions branch 3 times, most recently from 0de6e84 to bb6471e Compare July 7, 2025 09:58
@knocte
Copy link
Collaborator

knocte commented Jan 7, 2026

@webwarrior-ws let's rebase this, which should fix the CI to be green.

Added FavourNestedFunctions rule and tests for it.
Implemented FavourNestedFunctions rule. Added rule text message
to Text.resx.

Fixes fsprojects#638
@webwarrior-ws webwarrior-ws force-pushed the favour-nested-functions branch from 08a13a0 to 904ef9c Compare January 7, 2026 09:41
@knocte knocte changed the title DRAFT: Favour nested functions New rule: Favour Nested Functions Jan 7, 2026
Updated Configuration.fs and fsharplint.json to include
FavourNestedFunctions rule. Added docs.
@webwarrior-ws webwarrior-ws force-pushed the favour-nested-functions branch from 6397f05 to acf70ec Compare January 7, 2026 09:53
@knocte
Copy link
Collaborator

knocte commented Jan 7, 2026

@webwarrior-ws let's fix SelfCheck

@knocte
Copy link
Collaborator

knocte commented Jan 7, 2026

Damn, I just realised that this rule is also finding Literal values, which is not exactly bad, but then it means that the rule name is not the best: should we rename from FavourNestedFunctions to FavourLocalNestedOverPrivate?

@webwarrior-ws
Copy link
Contributor Author

Sometimes nesting functions will make the function too big. What to do in such cases? Remove private or disable the rule for specific function?

@webwarrior-ws
Copy link
Contributor Author

Damn, I just realised that this rule is also finding Literal values, which is not exactly bad, but then it means that the rule name is not the best: should we rename from FavourNestedFunctions to FavourLocalNestedOverPrivate?

Literals can't be nested inside functions. Only module-level.

@knocte
Copy link
Collaborator

knocte commented Jan 7, 2026

Literals can't be nested inside functions. Only module-level.

Then that's a false positive to fix before merging. Anyway, my point still stands wrt module elements that are not functions, just fields.

@knocte
Copy link
Collaborator

knocte commented Jan 7, 2026

Remove private

Remove private? Then the rule would be forcing you to expose your internals!!

disable the rule for specific function?

Sigh, we're not applying a rule suggestion to ignore another rule. Ignoring rules should be done in very specific scenarios where it cannot be avoided but in this case it looks like it would need too many ignores; therefore the proper way to fix this is tweak MaxLinesInFunction to not count nested function lines

That test recursive function definitions.
Fixed bug when rule was not correctly identifying functions
when they are declared using let rec ... and ... syntax.
@webwarrior-ws
Copy link
Contributor Author

Discovered another problem:
Using attributes on nested functions (e.g [<TailCall>]) will give syntax error:
Unexpected symbol '[<' in expression

@webwarrior-ws webwarrior-ws force-pushed the favour-nested-functions branch from ed0eeec to 97ee66d Compare January 7, 2026 13:01
That makes sure that literals are not triggering the rule.
Do not fire the rule for bindings with [<Literal>] attribute
because they can only be module-level.
@webwarrior-ws webwarrior-ws force-pushed the favour-nested-functions branch from 97ee66d to 0c5a4c3 Compare January 8, 2026 08:16
@webwarrior-ws
Copy link
Contributor Author

Discovered another problem: Using attributes on nested functions (e.g [<TailCall>]) will give syntax error: Unexpected symbol '[<' in expression

So maybe don't apply the rule if the function has any attributes?

@knocte
Copy link
Collaborator

knocte commented Jan 8, 2026

So maybe don't apply the rule if the function has any attributes?

Right, let's add a test for it.

That makes sure that functions with attributes are not
triggering the rule.
Because using attributes on nested functions (e.g [<TailCall>])
will give syntax error:
```
Unexpected symbol '[<' in expression
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

New rule FavourNestedFunctions

3 participants