Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

checker: check fn return void fn call (no value) (fix #22389) #22404

Merged
merged 1 commit into from
Oct 4, 2024

Conversation

yuyi98
Copy link
Member

@yuyi98 yuyi98 commented Oct 3, 2024

This PR check fn return void fn call (no value) (fix #22389).

  • Check fn return void fn call (no value).
  • Fix return match expr with result.
  • Add tests.
fn main() {
	install_command(['github:some/thing'], 1) or { panic(err) }
}

pub fn install_command(args []string, verbosity int) ! {
	if args.len == 0 {
		return error('${@FN} requires an argument')
	}
	component := args[0]
	if component.count(':') == 0 {
		return install_command(['github:${component}'], verbosity)
	}
	source := component.all_before(':')
	if source != 'github' {
		return error('${@FN} unknown source `${source}`')
	}
	unit := component.all_after(':')
	return match source {
		'github' {
			install_from_github(unit, verbosity)!
		}
		else {
			error('${@FN} unknown source `${source}`')
		}
	}
}

fn install_from_github(unit string, verbosity int) ! {
	dump(unit)
}

PS D:\Test\v\tt1> v run .    
tt1.v:20:4: error: `install_from_github(unit, verbosity)!` used as value
   18 |     return match source {
   19 |         'github' {
   20 |             install_from_github(unit, verbosity)!
      |             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   21 |         }
   22 |         else {
fn main() {
	install_command(['github:some/thing'], 1) or { panic(err) }
}

pub fn install_command(args []string, verbosity int) ! {
	if args.len == 0 {
		return error('${@FN} requires an argument')
	}
	component := args[0]
	if component.count(':') == 0 {
		return install_command(['github:${component}'], verbosity)
	}
	source := component.all_before(':')
	if source != 'github' {
		return error('${@FN} unknown source `${source}`')
	}
	unit := component.all_after(':')
	return match source {
		'github' {
			install_from_github(unit, verbosity)
		}
		else {
			error('${@FN} unknown source `${source}`')
		}
	}
}

fn install_from_github(unit string, verbosity int) ! {
	dump(unit)
}

PS D:\Test\v\tt1> v run .    
[.\\tt1.v:29] unit: some/thing

Copy link
Member

@spytheman spytheman left a comment

Choose a reason for hiding this comment

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

Excellent work.

@spytheman spytheman merged commit 884b842 into vlang:master Oct 4, 2024
78 checks passed
@yuyi98 yuyi98 deleted the check_return_no_value branch October 4, 2024 07:16
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.

bug in return match statement when result/option is involved
2 participants