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

Added checks for clang::Cursor::template_arg_value #215

Merged
merged 1 commit into from
Nov 6, 2016

Conversation

nikhilshagri
Copy link
Contributor

Fixes #134.
r? @fitzgen

@highfive
Copy link

highfive commented Nov 5, 2016

Thanks for the pull request, and welcome! The Servo team is excited to review your changes, and you should hear from @fitzgen (or someone else) soon.

@highfive
Copy link

highfive commented Nov 5, 2016

warning Warning warning

  • These commits modify unsafe code. Please review it carefully!

@emilio
Copy link
Contributor

emilio commented Nov 5, 2016

Is there any caller in this function? If not, let's rip it out.

fitzgen
fitzgen previously requested changes Nov 5, 2016
Copy link
Member

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

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

Thanks @cynicaldevil!

If we aren't using this method anywhere, then I agree with @emilio that the best thing to do is to remove the method (sorry for not catching this when filing the issue!). Would you like to morph this PR into the method's removal?

That said, I've left a couple comments for your benefit.

I would probably write this code in a way such that the bounds checking isn't "distant" from the FFI call, like this:

pub fn template_arg_value(&self, i: c_int) -> Option<c_longlong> {
    match self.num_template_args() {
        // Fold bounds check into match arm.
        Some(n) if 0 <= i && i < n => {
            // Ok, it is safe to make the FFI call.
            Some(unsafe {
                clang_Cursor_getTemplateArgumentValue(self.x, i as c_uint)
            })
        _ => None,
    }
}

One could also use https://doc.rust-lang.org/nightly/std/option/enum.Option.html#method.map or https://doc.rust-lang.org/nightly/std/option/enum.Option.html#method.and_then here instead of a match, if one preferred.

None => false
};
match within_bounds {
true => {
Copy link
Member

Choose a reason for hiding this comment

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

In general, using if is usually better than match for boolean values:

if within_bounds {
    ...
} else {
    ...
}

@nikhilshagri
Copy link
Contributor Author

@fitzgen I've removed the method and updated the PR. Think I'll pick up another issue to work on as well, since this one turned out to be really trivial.

@bors-servo
Copy link

☀️ Test successful - status-travis

@bors-servo bors-servo merged commit 0e63240 into rust-lang:master Nov 6, 2016
Copy link
Contributor

@wafflespeanut wafflespeanut left a comment

Choose a reason for hiding this comment

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

Could you also remove the c_longlong imported at the top? It's causing the warnings (which we don't allow).

@nikhilshagri
Copy link
Contributor Author

@wafflespeanut I had forgotten to to run the build step after removing the method. All good now!

@wafflespeanut wafflespeanut dismissed fitzgen’s stale review November 6, 2016 09:58

@cynicaldevil has made the changes :)

@wafflespeanut
Copy link
Contributor

@bors-servo r=emilio

@bors-servo
Copy link

📌 Commit 0e63240 has been approved by emilio

@bors-servo
Copy link

⌛ Testing commit 0e63240 with merge 09e6fe4...

@highfive highfive assigned emilio and unassigned fitzgen Nov 6, 2016
bors-servo pushed a commit that referenced this pull request Nov 6, 2016
Added checks for clang::Cursor::template_arg_value

Fixes #134.
r? @fitzgen
@nikhilshagri nikhilshagri deleted the template-check branch November 6, 2016 23:45
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.

6 participants